xUtils3 networking module

xUtils3 networking module
doing Android software, networking requesting network is essential to the function, xUtils3 package a variety of network-related functions,
network request text, and can use the Get or Post request text; also supports large file downloads than 2G, the most satisfactory function is also supported for HTTP, HTTP is what it is half after downloading the file, and then download the original part then download the file, so as not to re-download, saving the user traffic. xUtils3 also supports file upload, upload files larger than 2G also supports, in addition to video recording can be used xUtils3 upload a video, the user can xUtils3 upload avatar.

 

1_xUtils3NetActivity layout file 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"><!-标题栏 -->
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@android:color/holo_blue_light"
            android:gravity="center"
            android:orientation="horizontal">
        <TextView
                android:text="xUtils3 网络模块"
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textSize="20sp"/>
    </LinearLayout>
    <Button
            android:id="@+id/btn_get_post"
            android:text="get 和 pos 请求"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <Button android:id="@+id/btn_downloadfile"
            android:text="大文件下载"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <Button android:id="@+id/btn_uploadfile"
            android:text="文件上传"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    <TextView android:id="@+id/tv_result"
              android:text="显示结果"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
    <ProgressBar android:id="@+id/progressBar" 
                 style="?android:progressBarStyleHorizontal"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"/>
</LinearLayout>

1.1 instantiated layout 

 

@ContentView(R.layout.activity_net)
public class xUtils3NetActivity extends Activity {


    @ViewInject(R.id.tv_result)
    private TextView textView;

    @ViewInject(R.id.progressBar)
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }

    @Event(value = {R.id.btn_get_post, R.id.btn_downloadfile, R.id.btn_uploadfile})
    private void getEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_get_post:
                getDataByGet_Post();
                break;
            case R.id.btn_downloadfile:
                downloadfile();
                break;
            case R.id.btn_uploadfile:
                uploadfile();
                break;
        }
    }
} 

 

2_ use xUtils3 Get request text
Get request, there will be a callback after the success of this method is the most common requests textually

 

 private void getDataByGet_Post() {
        RequestParams request = new RequestParams("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
        x.http().get(request, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                LogUtil.e("onSuccess-result==" + result);
                Toast.makeText(xUtils3NetActivity.this, "result==" + result, Toast.LENGTH_SHORT).show();
                textView.setText(result + "");
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.e("onError==" + ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("onCancelled==" + cex.getMessage());
            }

            @Override
            public void onFinished() {
                LogUtil.e("onFinished==");
            }
        });
    }

3_ use xUtils3 of Post request Text 

  private void getDataByGet_Post() {
        RequestParams request = new RequestParams("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
        x.http().post(request, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                LogUtil.e("onSuccess-result==" + result);
                Toast.makeText(xUtils3NetActivity.this, "result==" + result, Toast.LENGTH_SHORT).show();
                textView.setText(result + "");
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.e("onError==" + ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("onCancelled==" + cex.getMessage());
            }

            @Override
            public void onFinished() {
                LogUtil.e("onFinished==");
            }
        });
    }

4_ use xUtils3 file download & HTTP
xUtils3 supports downloading files larger than 2G of the most pleasant surprise is also support for HTTP download.

1_ download file code

 Private  void DownloadFile () { 
        Final RequestParams reqest = new new RequestParams ( " http://vfx.mtime.cn/Video/2016/09/02/mp4/160902093947207009_480 MP4. " ); 
        reqest.setAutoRename ( to false ); // Set the header information of whether the file is automatically named      
        reqest.setSaveFilePath (Environment.getExternalStorageDirectory () + " /atguigu/oppo.mp4 " ); 
        reqest.setExecutor ( new new PriorityExecutor ( . 3 , to true ;))
         // custom thread pool, valid value range [1, 3], is set to 3, may block the image is loaded.     
        reqest.setCancelFast ( to true );
         // Can be stopped immediately. 

        x.http (). GET (reqest, new new Callback.ProgressCallback <File> () { 
            @Override 
            public  void the onSuccess (File Result) { 

                LogUtil.e ( " onSuccess- successful download file " + result.toString ()); 
            } 

            @Override 
            public  void the onError (the Throwable EX, Boolean isOnCallback) { 
                LogUtil.e ( " onError- failed download file " + ex.getMessage ()); 
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("onCancelled-");
            }

            @Override
            public void onFinished() {
                LogUtil.e("onFinished-");
            }

            @Override
            public void onWaiting() {
                LogUtil.e("onWaiting-");
            }

            @Override
            public void onStarted() {
                LogUtil.e("onStarted-");
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                progressBar.setMax((int) total);
                progressBar.setProgress((int) current);
                LogUtil.e("onLoading-" + current + "/" + total);
            }
        });
    }

2_ plus permission to remember 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

3_ set HTTP
red where the following is set up to support the local HTTP

 

 Private  void DownloadFile () { 
        Final RequestParams reqest = new new RequestParams ( " http://vfx.mtime.cn/Video/2016/09/02/mp4/160902093947207 009_480.mp4 " ); 
        reqest.setAutoRename ( to false );
         // settings are automatically named according to the file header information      
        reqest.setSaveFilePath (Environment.getExternalStorageDirectory () + " /atguigu/oppo.mp4 " ); 
        reqest.setAutoResume ( to true );
         // settings are automatically downloaded HTTP      
        reqest.setExecutor ( new new PriorityExecutor ( 3 , to true));
         // custom thread pool, valid value range [1, 3], is set to 3, may block the image is loaded.      
        Reqest.setCancelFast ( to true ); // Can be stopped immediately. 

        X.http () . GET (reqest, new new Callback.ProgressCallback <file> () { 
            @Override 
            public  void the onSuccess (file Result) { 

                LogUtil.e ( " onSuccess- successful download file " + result.toString ()); 
            } 

            @Override 
            public  void the onError (Throwable EX, boolean isOnCallback) { 
                LogUtil.e ( " onError- failed to download file" + ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("onCancelled-");
            }

            @Override
            public void onFinished() {
                LogUtil.e("onFinished-");
            }

            @Override
            public void onWaiting() {
                LogUtil.e("onWaiting-");
            }

            @Override
            public void onStarted() {
                LogUtil.e("onStarted-");
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                progressBar.setMax((int) total);
                progressBar.setProgress((int) current);
                LogUtil.e("onLoading-" + current + "/" + total);

            }
        });

    }

5_ large file upload
1_ supports file upload server built
using tomcat build a service that supports file upload


2_ file upload
file upload, it also sets the upload progress, but also need to rewrite this interface ProgressCallback

 

 private void uploadfile() {
        RequestParams params = new RequestParams("http://192.168.11.215:8080/FileUpload/FileUploadServlet");
        //使用 multipart 表单上传文件     
        params.setMultipart(true);
        params.addBodyParameter("file", new File(Environment.getExternalStorageDirectory() + "/atguigu/oppo.mp4"), null, "oppo.mp4");
        x.http().post(params, new Callback.ProgressCallback<File>() {
            @Override
            public void onSuccess(File result) {

                LogUtil.e("onSuccess-上传文件成功" + result.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.e("onError-下载文件失败" + ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("onCancelled-");
            }

            @Override
            public void onFinished() {
                LogUtil.e("onFinished-");
            }

            @Override
            public void onWaiting() {
                LogUtil.e("onWaiting-");
            }

            @Override
            public void onStarted() {
                LogUtil.e("onStarted-");
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                progressBar.setMax((int) total);
                progressBar.setProgress((int) current);
                LogUtil.e("onLoading-" + current + "/" + total);

            }
        });
    }

 

Guess you like

Origin www.cnblogs.com/yanglanwan/p/11305554.html