{
"camera_index":[0],
"action":"start",
"resolution":0
}
这是我要提交的数据,第一个参数为数组,第二个为String,第三个位int,
具体格式为
"application/json"
首先是服务器的代码,我把所有请求的方法都放在一个里面,HttoUtils.java中;
public static void post(String uri, JSONObject jsonObject, ResponseHandlerInterface responseHandler) { ByteArrayEntity entity = null; try { entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8")); entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Log.d("DEBUG", "URI地址:" + getFullURL(uri)); CookieStore cookieStore = (CookieStore) HttpUtils.getClient().getHttpContext().getAttribute(HttpClientContext.COOKIE_STORE); client.setCookieStore(cookieStore); System.out.println("HtttpUtils--cookieStore---------------------" + cookieStore.getCookies().get(0).getName() + cookieStore.getCookies().get(0)); client.post(mContext, getFullURL(uri), entity, "application/json", responseHandler); }
然后是具体的实现代码:
JSONArray jsonArray = new JSONArray(); jsonArray.put(0); JSONObject jsonObject = new JSONObject(); jsonObject.put("camera_index", jsonArray); jsonObject.put("action", "start"); jsonObject.put("resolution", 0); System.out.println("_____________________jsonObject" + jsonObject.toString()); HttpUtils.post(MaintenanceRecordHttp, jsonObject, new JsonHttpResponseHandler() { //第一个为url地址,第二个是打包后的数组 @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { Log.d("DEBUG", "_______****************__________" + response); int status = response.optInt("status"); String msg = response.optString("message"); String datas = response.optString("datas"); if (status == 20000) { Log.d("DEBUG", "UpdatePhotos-成功返回:" + response.toString()); Toast.makeText(AirPlayer.this, msg, Toast.LENGTH_SHORT).show(); } else { Log.d("DEBUG", "UpdatePhotos-Status=50003:" + response.toString()); Toast.makeText(AirPlayer.this, msg, Toast.LENGTH_SHORT).show(); } } });