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();

   }
}

    

activity_main.xml



   

你可能感兴趣的:(OkHttp)