溫馨提示×

溫馨提示×

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

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

使用Android Studio OkHttpClient的方法

發布時間:2020-10-30 18:49:55 來源:億速云 閱讀:920 作者:Leah 欄目:開發技術

使用Android Studio OkHttpClient的方法?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

首先記住,使用網絡的時候一定要加入權限,加入到AndroidMainfest.xml中

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

在初次使用的時候會出現報錯。cannot resolve symbol OkHttpClient

這里需要引入

implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下項目就可以了。

代碼:

package com.example.administrator.testclient;


import com.squareup.*;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class BaseHttpClient {

 public static final MediaType MEDIA_TYPE_MARKDOWN
   = MediaType.parse("text/x-markdown; charset=utf-8");
 // 01. 定義okhttp
 private final OkHttpClient client = new OkHttpClient();

 public BaseHttpClient(){

  //client.connectTimeoutMillis();
 }


 /**
  * 發送一個表單請求
  * @throws Exception
  */
 public void SendForm() throws Exception {
  RequestBody formBody = new FormBody.Builder()
    .add("search", "Jurassic Park")
    .build();
  Request request = new Request.Builder()
    .url("https://en.wikipedia.org/w/index.php")
    .post(formBody)
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);

  System.out.println(response.body().string());
 }

 /**POST 請求
  * 發送一個string請求
  * @throws Exception
  */
 public void SendPostString() throws Exception {
  String postBody = ""
    + "Releases\n"
    + "--------\n"
    + "\n"
    + " * _1.0_ May 6, 2013\n"
    + " * _1.1_ June 15, 2013\n"
    + " * _1.2_ August 11, 2013\n";

  Request request = new Request.Builder()
    .url("https://api.github.com/markdown/raw")
    .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);

  System.out.println(response.body().string());
 }

 /**POST 請求
  * 發送一個From請求
  * @throws Exception
  */
 public void SendPostFrom() throws Exception {

  RequestBody body = new FormBody.Builder()
    .add("name", "sy")//添加參數體
    .add("age", "18")
    .build();

  Request request = new Request.Builder()
    .post(body) //請求參數
    .url("http://123.207.70.54:8080/SpringMvc/hello")
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);
 }

 /**Get請求
  * 發送一個From請求
  * @throws Exception
  */
 public void SendGetFrom() throws Exception {

  Request request = new Request.Builder()
    .get() //請求參數
    .url("http://123.207.70.54:8080/SpringMvc/hello")
    .build();

  Response response = client.newCall(request).execute();
  if (!response.isSuccessful())
   throw new IOException("Unexpected code " + response);
 }

}

測試發現,上面的用不了,下面放一個測試通過的方法:

public void getDatasyncFactory(){
    new Thread(new Runnable() {
     @Override
     public void run() {
      try {
       OkHttpClient client = new OkHttpClient();//創建OkHttpClient對象
       Request request = new Request.Builder()
         .url("http://123.207.70.54:8080/SpringMvc/hello")//請求接口。如果需要傳參拼接到接口后面。
         .build();//創建Request 對象
       Response response = null;
       response = client.newCall(request).execute();//得到Response 對象
       if (response.isSuccessful()) {
        Log.d("kwwl","response.code()=="+response.code());
        Log.d("kwwl","response.message()=="+response.message());
        Log.d("kwwl","res=="+response.body());
        //此時的代碼執行在子線程,修改UI的操作請使用handler跳轉到UI線程。
       }
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }).start();
   }

返回信息:

使用Android Studio OkHttpClient的方法

看完上述內容,你們掌握使用Android Studio OkHttpClient的方法的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女