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)可以设置唯一的name、value。如果已经有值,旧的将被移除,然后添加新的。使用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); } }); } });