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 请求,构造Request对象,用过前两步中的对象构建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();   //不可执行多次io 读取完流关闭
                   System.out.println(s);


                   //POST 请求 ,与GET相比,就是在构造Request 对象时 ,需要多构造一个RequestBody 对象,
                   // 用它来携带我们要提交的数据。在构造RequestBody 需要指定MediaType,用于描述请求/响应body的内容类型
                   OkHttpClient client =new OkHttpClient();
                   String url="http://www.lnfvc.edu.cn/newweb/index.asp";
                   String s="{\"name\":\"Http权威指南\"}";
                   //设置请求体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">
<!--发送HTTP请求-->
   <Button
       android:id="@+id/send_request"
       android:text="send request"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
   <!--以滚动的形式查看屏幕外的内容-->
   <ScrollView
       android:layout_width="match_parent"
       android:layout_height="match_parent">

   </ScrollView>
   <!--将服务器返回的数据显示出来-->
   <TextView
       android:id="@+id/response_text"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       />

</LinearLayout>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容