1. 程式人生 > >Android okhttp 三種請求方式 get put post

Android okhttp 三種請求方式 get put post

開發中遇到用okhttp 請求獲取資料,剛做完一點,過程中遇到過一些坑,不過現在都解決了,再次記錄一下,直接上程式碼:

get方式的請求

new Thread() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();

                String format = String.format("ip地址加引數拼接");
                //類似  KeyPath.Path.head + KeyPath.Path.smsalarm, username, userPass, type, lat, lon, finalOptions, text10            KeyPath.Path.head + KeyPath.Path.smsalarm是封裝好的ip地址    後面是引數
Request build1 = new Request.Builder().url(format).build(); okHttpClient.newCall(build1).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override
public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); if (string != null) { try { final JSONObject jsonObject = new
JSONObject(string); int status = jsonObject.getInt("status"); if (status == 0) {//介面返回正確 mHandler.post(new Runnable() { @Override public void run() { } catch (JSONException e) { e.printStackTrace(); } } }); } } catch (JSONException e) { e.printStackTrace(); } } } }); } }.start();

post請求

new Thread() {
                        @Override
                        public void run() {

                            OkHttpClient okHttpClient = new OkHttpClient();
                            FormBody formBody = new FormBody.Builder()
                                    //add要拼接的form表單
                                    .add("carnumber", text1)
                                    .build();

                            String format = String.format("ip地址拼接+引數");
//類似  KeyPath.Path.head + KeyPath.Path.smsalarm, username, userPass, type, lat, lon, finalOptions, text10            KeyPath.Path.head + KeyPath.Path.smsalarm是封裝好的ip地址    後面是引數
                            Request request = new Request.Builder()
                                    .url(format)
                                    .post(formBody)
                                    .build();
                            okHttpClient.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {
                                    //"資料獲取失敗,請重新嘗試!"
                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    String string = response.body().string();
                                    if (string != null) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(string);
                                            int status = jsonObject.getInt("status");
                                            if (status == 1) {
                                                mHandler.post(new Runnable() {
                                                    @Override
                                                    public void run() {
//                                                       "傳送成功!"
                                                    }
                                                });
                                            } else {
                                                mHandler.post(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        // "傳送失敗,請稍後重試!
                                                    }
                                                });
                                            }

                                        } catch (JSONException e) {

                                        }
                                    }
                                }
                            });
                        }
                    }.start();

put請求

new Thread() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                FormBody build = new FormBody.Builder()
                        .add("Rfid", Rfid)
                        .add("state", "1")
                        .add("u", username)
                        .add("key", key)
                        .add("utc", current_timestamp)
                        .build();
                String format = String.format("http://192.168.1.76:9299/api/iceplate/setstate/%s?state=%s&api_key=&api_key=&u=%s&key=%s&utc=%s",Rfid, 1, username, key, current_timestamp);
                Request build1 = new Request.Builder()
                        .url(format)
                        .put(build)
                        .build();

                okHttpClient.newCall(build1).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String string = response.body().string();
                        if (string != null) {
                            try {
                                final JSONObject jsonObject = new JSONObject(string);
                                int status = jsonObject.getInt("status");
                                if (status == 0) {
                                    mHandler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(Home_Thelibrary.this, "修改狀態成功!", Toast.LENGTH_SHORT).show();
                                            show();
                                        }
                                    });
                                }else {
                                    mHandler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(Home_Thelibrary.this, "修改狀態失敗,請稍後重試!", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            } finally {
                                progressDlgEx.closeHandleThread();
                            }
                        }
                    }
                });

            }
        }.start();

這只是本人開發過程中自己琢磨和學習的方式,如果有不對的地方,歡迎指正。