httpClient的patch请求代码实现

2017-03-10  本文已影响0人  大力水手三号

@SuppressWarnings("unused")

public static JSONObject httpPatch(String Authorization, JSONObject jsonParam, String url) {

JSONObject resultObj = null;

String strResult = "";

@SuppressWarnings("deprecation")

HttpClient httpClient = new DefaultHttpClient();

HttpPatch httpPatch = new HttpPatch(url);

httpPatch.setHeader("Content-type", "application/json");

httpPatch.setHeader("Charset", HTTP.UTF_8);

httpPatch.setHeader("Accept", "application/json");

httpPatch.setHeader("Accept-Charset", HTTP.UTF_8);

httpPatch.setHeader("Authorization", Authorization);

if (jsonParam == null) {

return resultObj; 

}

try {

StringEntity entity = new StringEntity(jsonParam.toString(), HTTP.UTF_8);

System.out.println("entity:" + entity.toString());

httpPatch.setEntity(entity);

HttpResponse response = httpClient.execute(httpPatch);

int statusCode = response.getStatusLine().getStatusCode();

switch (statusCode) {

case HttpStatus.SC_OK:

/**

* 读取服务器返回过来的json字符串数据*

*/

strResult = EntityUtils.toString(response.getEntity());

System.out.println("strResult:" + strResult);

resultObj = JSONObject.fromObject(strResult);

break;

case HttpStatus.SC_METHOD_FAILURE:

// 其他一些处理

resultObj = null;

break;

// ..........还有好多状态,可以按照需要处理

default:

resultObj = null;

break;

}

} catch (ParseException | JSONException | IOException e) {

e.printStackTrace();

return null;

}

return resultObj;

}

上一篇下一篇

猜你喜欢

热点阅读