Team Development Sprint Day (ten)

  HttpURLConnection still failed to upload pictures and files at the same time. After unsuccessful attempts, I looked at the upload method of OKHttp and summarized below:

  Reference blog:

  https://blog.csdn.net/nsplnpbjy/article/details/104690131

  https://blog.csdn.net/yancychas/article/details/78212701

  https://blog.csdn.net/gxflh/article/details/81133337?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

  The idea of ​​using OKHttp is similar to HttpURLConnection, mainly in setting multiple parameters, you can use Request.Builder, such as the following code:

  Original address: https://www.cnblogs.com/shenchanghui/p/6407271.html

 1  public void call(Subscriber<? super String> subscriber) {
 2                 MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder();
 3                 multipartBodyBuilder.setType(MultipartBody.FORM);
 4                 //遍历map中所有参数到builder
 5                 if (params != null){
 6                     for (String key : params.keySet()) {
 7                         multipartBodyBuilder.addFormDataPart(key, params.get(key));
 8                     }
 9                 }
10                 //Traverse the absolute path of all the pictures in the paths to the builder, and agree on the key such as "upload" as the background to accept multiple pictures as the key 
11                  if (files! = Null ) {
 12                      for (File file: files) {
 13                          multipartBodyBuilder.addFormDataPart (pic_key, file.getName (), RequestBody.create (MEDIA_TYPE_PNG, file));
 14                      }
 15                  }

Builder is used here to encapsulate multiple parameters.

Basic usage reference: https://www.jianshu.com/p/da4a806e599b

 1 MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
 2 String requestBody = "I am Jdqm.";
 3 Request request = new Request.Builder()
 4         .url("https://api.github.com/markdown/raw")
 5         .post(RequestBody.create(mediaType, requestBody))
 6         .build();
 7 OkHttpClient okHttpClient = new OkHttpClient();
 8 okHttpClient.newCall(request).enqueue(new Callback() {
 9     @Override
10     public void onFailure(Call call, IOException e) {
11         Log.d(TAG, "onFailure: " + e.getMessage());
12     }
13 
14     @Override
15     public void onResponse(Call call, Response response) throws IOException {
16         Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
17         Headers headers = response.headers();
18         for (int i = 0; i < headers.size(); i++) {
19             Log.d(TAG, headers.name(i) + ":" + headers.value(i));
20         }
21         Log.d(TAG, "onResponse: " + response.body().string());
22     }
23 });

Note that the default is a GET request. The use of POST requests requires a line 4 statement.post ()

The next step is the actual combat stage, to see if you can use OKHttp to achieve background functions, in addition, the sprint date is extended to next Monday, come on.

 

Guess you like

Origin www.cnblogs.com/20183711PYD/p/12754874.html