Android post 提交json数组到服务器

{
    "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();
        }
    }
});
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值