91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android-Okhttp的使用解析

發布時間:2020-09-27 21:51:40 來源:腳本之家 閱讀:134 作者:蕙心紈質 欄目:移動開發

okhttp是Android6.0推出的網絡框架。由于谷歌在Android6.0的之后,將HttpClient相關屬性取消掉,導致Volley框架不能正常使用。所以才有了今天的Okhttp。

 Okhttp進行網絡訪問通常有兩種方式,一種是get請求,還有一種叫做post請求。

1、OKhttp的get請求

通常,我們使用get方式來請求一個網站,是依靠url地址的。Okhttp使用get方式來請求網站通常有如下的步驟:

A、創建OkhttpClient的變量,這個變量相當于是一個全局的執行者。主要的網絡操作是依靠它來進行的。

B、創建一個builder對象。

C、利用builder對象創建一個Request對象。

D、使用全局執行者來創建一個Call對象。

E、通過Call對象來進行網絡連接。

public void doGet(View view)
  {
    Request.Builder builder = new Request.Builder();
    Request request = builder.get().url(urlString + "userName=pby&userPassword=123").build();
    Call newCall = mOkHttpClient.newCall(request);
    //newCall.execute()
    newCall.enqueue(new Callback() {
      @Override
      public void onFailure(Request request, IOException e) {
        L.e("失敗了");
      }

      @Override
      public void onResponse(Response response) throws IOException {
        String string = response.body().string();
        L.e(string);
      }
    });
  }

2、Okhttp的Post請求

Post請求與get請求有些不一樣。get請求主要的功能是從服務器上獲取數據,而Post請求則是向服務器提交數據。

public void doPost(View view)
  {
    FormEncodingBuilder requestBodyBuilder = new FormEncodingBuilder();
    RequestBody requestBody = requestBodyBuilder.add("userName", "pby").add("userPassword", "123").build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlString).post(requestBody).build();
    Call newCall = mOkHttpClient.newCall(request);
    executeCall(newCall);
  }

3、服務器端接收客戶端傳過來的字符串

客戶端的代碼:

public void doPostString(View view)
  {
    RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset = utf-8"), "{name = pby, password = 1234}");
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlString + "doPostString").post(requestBody).build();
    Call newCall = mOkHttpClient.newCall(request);
    executeCall(newCall);
  }

服務器端的代碼:

public String doPostString() throws IOException
  {
    HttpServletRequest request = ServletActionContext.getRequest();
    ServletInputStream inputStream = request.getInputStream();
    StringBuilder sb = new StringBuilder();
    int len = 0;
    byte []buff = new byte[1024];
    while((len = inputStream.read(buff)) != -1)
    {
      sb.append(new String(buff, 0, len));
    }
    System.out.println(sb.toString());
    return null;
  }

服務器端如果要接收客戶端的數據,則需要接收request;如果服務器端想要給客戶端傳數據,則需要通過response來傳遞。

4、使用post方式進行文件的傳輸

客戶端的代碼

public void doPost(View view)
  {
    FormEncodingBuilder requestBodyBuilder = new FormEncodingBuilder();
    RequestBody requestBody = requestBodyBuilder.add("userName", "pby").add("userPassword", "123").build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlString + "login").post(requestBody).build();
    Call newCall = mOkHttpClient.newCall(request);
    executeCall(newCall);
  }

關于選擇文件的代碼--抄襲網絡上的代碼,并不是自己寫的

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
      startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), 1);
    } catch (android.content.ActivityNotFoundException ex) {
      Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
      case 1:
        if (resultCode == RESULT_OK) {
          // Get the Uri of the selected file
          Uri uri = data.getData();
          String path = FileUtils.getPath(this, uri);
          if(path != null)
          {
            postFile(path);
          }
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

在進行這個的操作的時候,一定要記住增加讀和寫的權限,否則會上傳失敗的。

服務器端的代碼

public String doPostFile() throws IOException
  {
    HttpServletRequest request = ServletActionContext.getRequest();
    ServletInputStream inputStream = request.getInputStream();
    String dir = ServletActionContext.getServletContext().getRealPath("files");
    File file = new File(dir, "abc.jpg");
    FileOutputStream fos = new FileOutputStream(file);
    int len = 0;
    byte [] buff = new byte[1024];
    while((len = inputStream.read(buff)) != -1)
    {
      fos.write(buff, 0, len);
    }
    fos.flush();
    fos.close();
    return null;
  }

上面顯示的files文件,在Tomcat的webapps下的工程名名文件下的fies文件夾(才開始是沒有這個文件夾的,需要手動自己創建)。

5.使用Post方式來上傳文件

客戶端代碼:

private void upLoadFile(String path)
  {
    File file = new File(path);
    if(!file.exists())
    {
      return ;
    }
    MultipartBuilder multipartBuilder = new MultipartBuilder();
    RequestBody requestBody = multipartBuilder.type(MultipartBuilder.FORM)
        .addFormDataPart("userName", "pby")
        .addFormDataPart("userPassword", "123")
        .addFormDataPart("mFile", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file)).build();
//    CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.MyListener() {
//      @Override
//      public void onRequestProgress(int byteWriteCount, int TotalCount) {
//        L.e(byteWriteCount + " / " + TotalCount);
//      }
//    });
    Request.Builder builder = new Request.Builder();
    //Request request = builder.url(urlString + "doUpLoadFile").post(countingRequestBody).build();
    Request request = builder.url(urlString + "doUpLoadFile").post(requestBody).build();
    Call newCall = mOkHttpClient.newCall(request);
    executeCall(newCall);
  }

服務器端的代碼:

public String doUpLoadFile()
  {
    if(mFile == null)
    {
      System.out.println(mFileFileName+" is null..");
      return null;
    }
    String dir = ServletActionContext.getServletContext().getRealPath("files");
    File file = new File(dir, mFileFileName);
    try {
      FileUtils.copyFile(mFile, file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }

在上傳文件的時候,有一個小細節都注意到:就是Tomcat服務器只允許上傳2m以下的文件。要想上傳大文件,就必須在struct文件中加一句:<constant name="struts.multipart.maxSize" value="1024000000"/>數字表示自定義大小的限制。

6.上傳文件時,進度的顯示問題

在寫代碼的時候我們知道,我們不能直接獲得上傳文件的進度。因為這些數據都是封裝在RequestBody里面的,要想使用只有通過回調接口來實現。

package com.example.android_okhttp;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;

import java.io.IOException;

import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;

/**
 * Created by 前世訣別的一紙書 on 2017/3/5.
 */

public class CountingRequestBody extends RequestBody {
  private RequestBody delegate = null;
  private MyListener mListener= null;
  private CountingSink mCountSink = null;
  public interface MyListener
  {
    void onRequestProgress(int byteWriteCount, int TotalCount);
  }
  public CountingRequestBody(RequestBody requestBody, MyListener listener)
  {
    delegate = requestBody;
    mListener = listener;
  }
  @Override
  public MediaType contentType() {
    return delegate.contentType();
  }

  @Override
  public void writeTo(BufferedSink sink) throws IOException {
    mCountSink = new CountingSink(sink);
    BufferedSink bs = Okio.buffer(mCountSink);
    delegate.writeTo(bs);
    bs.flush();
  }
  private class CountingSink extends ForwardingSink{
    private int byteWriteCount = 0;
    public CountingSink(Sink delegate) {
      super(delegate);
    }

    @Override
    public void write(Buffer source, long byteCount) throws IOException {
      super.write(source, byteCount);
      byteWriteCount += byteCount;
      mListener.onRequestProgress(byteWriteCount, (int) contentLength());
    }
  }

  @Override
  public long contentLength() throws IOException {
    return delegate.contentLength();
  }
}

MultipartBuilder multipartBuilder = new MultipartBuilder();
    RequestBody requestBody = multipartBuilder.type(MultipartBuilder.FORM)
        .addFormDataPart("userName", "pby")
        .addFormDataPart("userPassword", "123")
        .addFormDataPart("mFile", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file)).build();
    CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.MyListener() {
      @Override
      public void onRequestProgress(int byteWriteCount, int TotalCount) {
        L.e(byteWriteCount + " / " + TotalCount);
      }
    });
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlString + "doUpLoadFile").post(countingRequestBody).build();
    //Request request = builder.url(urlString + "doUpLoadFile").post(requestBody).build();
    Call newCall = mOkHttpClient.newCall(request);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

荔波县| 简阳市| 达日县| 方城县| 古浪县| 双江| 汽车| 普安县| 九江县| 新沂市| 沙河市| 长顺县| 保定市| 韶关市| 板桥市| 安远县| 克拉玛依市| 海林市| 彝良县| 大港区| 巴彦县| 鸡西市| 铜山县| 桐城市| 平潭县| 凤山县| 马关县| 宾阳县| 民县| 鄂伦春自治旗| 淅川县| 永和县| 城口县| 龙南县| 湾仔区| 新野县| 英吉沙县| 乌鲁木齐县| 阿拉善左旗| 安西县| 孟津县|