用HttpURLConnection,okhttp,httpclient传JSON数据

首先说httpclient

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://172.20.10.1:8080/transportservice/action/GetAllSense.do");
JSONObject object = new JSONObject();
object.put("UserName","Z0004");
StringEntity se = new StringEntity( object.toString(),"UTF-8");
//se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String msg;
while ((msg = bufferedReader.readLine()) != null) {
    stringBuffer.append(msg);
}

bufferedReader.close();
inputStreamReader.close();
inputStream.close();



    JSONObject jb = new JSONObject(stringBuffer.toString());
    two = new TwoEntry();
    two.setTemperature(jb.getInt("temperature"));
    two.setHumidity(jb.getInt("humidity"));
    two.setLightIntensity(jb.getInt("LightIntensity"));
    two.setCo(jb.getInt("co2"));
    two.setPm(jb.getInt("pm2.5"));


Message message = new Message();
message.obj = two;
message.what = 200;
handler.sendMessage(message);


接下来是okhttp

OkHttpClient client = new OkHttpClient();
String json = "{\"CarId\":" + Integer.valueOf(cid)
        + ",\"CarAction\":\"" + caction
        + "\",UserName\":\"" + cname
        + "\"}";
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url("http://172.20.10.1:8080/transportservice/action/SetCarMove.do")
        .post(body)
        .build();

Response response = client.newCall(request).execute();
String result = response.body().string();
JSONObject object = new JSONObject(result);
String re = object.getString("RESULT");
Message message = new Message();
message.obj = re;
handler.sendMessage(message);


最后是HttpURLConnection

URL url = new URL("http://172.20.10.1:8080/transportservice/action/SetCarMove.do");
//{"CarId":1, "CarAction":"Stop", "UserName":"Z0004"}
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

JSONObject json=new JSONObject();
json.put("CarId",Integer.valueOf(cid));
json.put("CarAction",caction);
json.put("UserName",cname);

conn.setRequestProperty("Content-Type", "application/json");


OutputStream outputStream = conn.getOutputStream();

outputStream.write(json.toString().getBytes());
outputStream.flush();
outputStream.close();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
//String data="CarId="+cid+"&CarAction="+caction+"&UserName="+cname;
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String msg;
while ((msg = br.readLine()) != null) {
    sb.append(msg);
}
br.close();
is.close();
isr.close();

JSONObject jb = new JSONObject(sb.toString());
final String result = jb.getString("RESULT");

Message mess = new Message();
mess.obj = result;
mess.what = 200;

猜你喜欢

转载自blog.csdn.net/haojiagou/article/details/79782100