okhttp 3.2バージョンの使用概要

21449 ワード

POSTはJsonデータを提出します。
 String url = "http://123.56.153.172:8801/NewSheet/NewSheet";//        
        try {
            OkHttpClient client = new OkHttpClient();
            client.newBuilder()
                    .connectTimeout(3, TimeUnit.SECONDS)//      
                    .readTimeout(30, TimeUnit.SECONDS)//    
                    .writeTimeout(30, TimeUnit.SECONDS)//    
                    .build();
            final JSONObject objInfo = new JSONObject();
//            JSONObject objInfoData = new JSONObject();// json 
//            objInfoData.put("offlineshopid", iGlobalParams.Offline_Shopid);
//            objInfoData.put("onSell", String.valueOf(1));
//            objInfoData.put("pageSize", String.valueOf(10));
//            objInfoData.put("pageNow", pageIndex);
//            objInfo.put("data", objInfoData);
            objInfo.put("MainID", String.valueOf(1));
            objInfo.put("ClientID", String.valueOf(""));
            objInfo.put("WriteID", String.valueOf(3));
            objInfo.put("WriteTime", "2016/4/20 17:26:17");
            objInfo.put("WriteAdr", "   ");
            objInfo.put("SheetType", String.valueOf(""));
            objInfo.put("SheetTitle", "   ");
            objInfo.put("SheetDetail", "    ");
            objInfo.put("SheetPriority", String.valueOf(""));
            objInfo.put("SheetState", String.valueOf(""));
            objInfo.put("TeamID", String.valueOf(""));
            objInfo.put("AcceptID", String.valueOf(""));
            objInfo.put("FollowID", String.valueOf(""));


//                       (   )
//                    FormBody formBody = new FormBody.Builder()
//                            .add("MainID", "1")
//                            .add("ClientID", "2")
//                            .add("WriteID", "3")
//                            .add("WriteTime", "4")
//                            .add("WriteAdr", "    ")
//                            .build();

//                    json   
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                    objInfo.toString());
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)//  json requestBody       formBody
                    .build();
            client.newCall(request)
                    .enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            handler.post(new Runnable() {//               Runnable    
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "     ", Toast.LENGTH_SHORT).show();

                                    Log.d("lxs", "     ");//    
                                }
                            });
                        }

                        @Override
                        public void onResponse(Call call, final Response response) throws IOException {

                            System.out.println("--------json-------" + objInfo.toString());
                            handler.post(new Runnable() {
                                public void run() {
                                    Toast.makeText(MainActivity.this, "     ", Toast.LENGTH_SHORT).show();
                                    // Log.d("lxs", "     ------    ");//    
                                    try {
                                        final String s = response.body().string();
                                        System.out.println("---------------------" + s);
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {

                                                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                             }
                        });
        } catch (JSONException e) {
            e.printStackTrace();
        }
注意:この中はhandlerを結び付けて使う必要があります。 
//okhttp          looper
private Handler handler = new Handler(Looper.getMainLooper());
get提出
new Thread(){
    @Override
    public void run() {
        //      
        Request request = new Request.Builder().url(url).build();
        try {
            //     
            ResponseBody body =  clientget.newCall(request).execute().body();
           String s =  body.string();
            System.out.println(""+s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}.start();
post     
  RequestBody requestBody = RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"),"dsahsadsdashjkjdashd");

                Request request = new Request.Builder()
                        .url("http://192.168.1.149:8801/NewSheet/NewSheet")
                        //  header(name, value)       namevalue。      ,      ,      。  addHeader(name, value)      (  ,      )。
                        // .addHeader("","")
//                        .header("User-Agent", "OkHttp Headers.java")
//                        .addHeader("Accept", "application/json; q=0.5")
//                        .addHeader("Accept", "application/vnd.github.v3+json")
                        .post(requestBody)
                        .build();
                final Call call= client1.newCall(request);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Toast.makeText(MainActivity.this, "    ", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String s = response.body().string();
                                System.out.println("    " + s);
                            }
                        });
                    }

                });