OkHttp

OkHttp是 HTTP 框架,它支持get 請求和post請求
MainActivity.java端代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   TextView responseText;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Button sendRequest=findViewById(R.id.send_request);
       responseText=findViewById(R.id.response_text);
       sendRequest.setOnClickListener(this);
   }
   @Override
   public void onClick(View v) {
       if (v.getId()==R.id.send_request){
           sendRequestWithOkHttp();
       }
   }
//同步請求
   private void sendRequestWithOkHttp() {
       new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   //GET 請求,構(gòu)造Request對象,用過前兩步中的對象構(gòu)建Call對象,最后通過 execute() 來提交請求
                   OkHttpClient client=new OkHttpClient();
                   //Request是OkHttp中訪問的請求,Buider是輔助類,Response即 OKHttp中的響應
                   String url="http://www.lnfvc.edu.cn/newweb/index.asp";
                   Request request=new Request.Builder().url(url).build();
                  Response response=client.newCall(request).execute();
                   String s=response.body().string();
                   response.body();   //不可執(zhí)行多次io 讀取完流關(guān)閉
                   System.out.println(s);


                   //POST 請求 ,與GET相比,就是在構(gòu)造Request 對象時 ,需要多構(gòu)造一個RequestBody 對象,
                   // 用它來攜帶我們要提交的數(shù)據(jù)。在構(gòu)造RequestBody 需要指定MediaType,用于描述請求/響應body的內(nèi)容類型
                   OkHttpClient client =new OkHttpClient();
                   String url="http://www.lnfvc.edu.cn/newweb/index.asp";
                   String s="{\"name\":\"Http權(quán)威指南\"}";
                   //設置請求體Media Type 指定媒體類型
                   RequestBody body=RequestBody.create(MediaType.parse("application/s;charset=utf-8"),s);
                   Request request=new Request.Builder()
                           .url(url)
                           .post(body) //等同 method("POST",body)請求方法需要全部大寫
                           .build();
                   Response response=null;

                   response=client.newCall(request).execute();
                   String x=response.body().string();
                   System.out.println(x);
 //異步處理 GET請求
                   OkHttpClient client=new OkHttpClient();
                   String url="http://www.lnfvc.edu.cn/newweb/index.asp";
                   Request request=new Request.Builder().url(url).build();
                   client.newCall(request).enqueue(new Callback() {
                       @Override
                       public void onFailure(Call call, IOException e) {
                           System.out.println(e);
                       }
                       @Override
                       public void onResponse(Call call, Response response) throws IOException {
                           String x=response.body().string();
                           System.out.println(x);
                       }
                   });


                   //POST請求
                   OkHttpClient client=new OkHttpClient();
                   String url="http://www.lnfvc.edu.cn/newweb/index.asp";
                   String s="{\"name\":\"JAVA 核心思想\"}";
                   RequestBody requestBody=RequestBody.create(MediaType.parse("applicaton/s;charset-uft-8"),s);
                   Request request=new Request.Builder().method("POST",requestBody).url(url).build();
                   client.newCall(request).enqueue(new Callback() {
                       @Override
                       public void onFailure(Call call, IOException e) {
                           System.out.println(e);
                       }
                       @Override
                       public void onResponse(Call call, Response response) throws IOException {
                           String s=response.body().string();
                           System.out.println(s);
                       }
                   });
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }).start();

   }
}

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

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lnjr.okhttp.MainActivity"
    android:orientation="vertical">
<!--發(fā)送HTTP請求-->
   <Button
       android:id="@+id/send_request"
       android:text="send request"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
   <!--以滾動的形式查看屏幕外的內(nèi)容-->
   <ScrollView
       android:layout_width="match_parent"
       android:layout_height="match_parent">

   </ScrollView>
   <!--將服務器返回的數(shù)據(jù)顯示出來-->
   <TextView
       android:id="@+id/response_text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       />

</LinearLayout>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容