正文中的 http PUT:请求错误,无法解析 API 令牌



我在解析和发送请求时遇到了一个大问题。在请求的正文中,我必须发送一个我在认证时已经成功获得的令牌。这是一份文件:

PUT/api/ver1/orders{"token":String,"order":Object}

下面是我如何解析和获取JSONObject:

JSONObject jsonObject = new JSONObject();
JSONObject params = new JSONObject();
params.put(ICConst.ORDER_TYPE, 1);
params.put(ICConst.PAYMENT, 2);
params.put(ICConst.ORDER_NAME, ICApplication.currentOrder .getOrderName());
params.put(ICConst.ORDER_DESCRIPTION, ICApplication.currentOrder .getOrderDescription());
JSONObject addressFrom = new JSONObject();
addressFrom.put(ICConst.CITY_FROM, ICApplication.currentOrder .getCityFrom());
addressFrom.put(ICConst.ADDRESS_FROM, ICApplication.currentOrder .getAddressFrom());
params.put(ICConst.FROM, addressFrom);
JSONObject periodFrom = new JSONObject();
periodFrom.put(ICConst.DATE_FROM, ICApplication.currentOrder .getDateFrom());
periodFrom.put(ICConst.TIME_FROM_START, ICApplication.currentOrder .getTimeFromStart());
periodFrom.put(ICConst.TIME_FROM_TILL, ICApplication.currentOrder .getTimeFromTill());
params.put(ICConst.FROM_PERIOD, periodFrom);
JSONObject addressTo = new JSONObject();
addressTo.put(ICConst.CITY_TO, ICApplication.currentOrder .getCityTo());
addressTo.put(ICConst.ADDRESS_TO, ICApplication.currentOrder .getAddressTo());
params.put(ICConst.TO, addressTo);
JSONObject periodTo = new JSONObject();
periodTo.put(ICConst.DATE_TO, ICApplication.currentOrder .getDateTo());
periodTo.put(ICConst.TIME_TO_START, ICApplication.currentOrder .getTimeToStart());
periodTo.put(ICConst.TIME_TO_TILL, ICApplication.currentOrder .getTimeToTill());
params.put(ICConst.TO_PERIOD, periodTo);
JSONObject sender = new JSONObject();
JSONArray senderPhone = new JSONArray();
sender.put(ICConst.SENDER_NAME, ICApplication.currentOrder .getSenderName());
senderPhone.put(0, ICApplication.currentOrder .getSenderPhone());
sender.put(ICConst.SENDER_PHONE, senderPhone);
params.put(ICConst.SENDER, sender);
JSONObject recipient = new JSONObject();
JSONArray recipientPhone = new JSONArray();
recipient.put(ICConst.RECIPIENT_NAME, ICApplication.currentOrder .getRecipientName());
recipientPhone.put(0, ICApplication.currentOrder .getRecipientPhone());
recipient.put(ICConst.RECIPIENT_PHONE, recipientPhone);
params.put(ICConst.RECIPIENT, recipient);
JSONObject receiver = new JSONObject();
receiver.put(ICConst.RECEIVED_NAME, ICApplication.currentOrder .getReceiverComment());
receiver.put(ICConst.RECEIVED_COMMENT, ICApplication.currentOrder .getReceiverComment());
params.put(ICConst.RECEIVED_BY, receiver);
params.put(ICConst.CARGO, getCargo());
jsonObject.put("order", params);
jsonObject.put("token", ICApplication.currentProfile.getToken());

这是Httpput请求

HttpPut httpPut = new HttpPut(getAbsoluteUrl(url));
httpPut.setHeader(HTTP.CONTENT_TYPE,
"application/x-www-form-urlencoded");
String str = String.valueOf(jsonObject);
StringEntity entity = new StringEntity(str, "UTF-8");
entity.setContentType("application/json");
entity.setContentType("application/x-www-form-urlencoded");
httpPut.setEntity(entity);
HttpResponse response = httpclient.execute(httpPut);
String request = inputStreamToString(response.getEntity().getContent());
Log.v("requestStringEntity", entity + "!");
Log.v("request", request + "!");

另一个变体是当我使用com.loopj.android.http.AsyncHttpClient时。我没有其他获取、修补和发布请求,除了PUT之外,一切都很成功。这是我得到的代码:

public static void put(Context context, String url, JSONObject jsonObject, AsyncHttpResponseHandler handler) {
StringEntity entity = null;
try {
entity = new StringEntity(jsonObject.toString());
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
} catch (UnsupportedEncodingException _e) {
_e.printStackTrace();
}
client.setURLEncodingEnabled(true);
client.put(context, getAbsoluteUrl(url), entity, "application/json", handler);
}

这里是entuty/jsonObject字符串:

{"代币":"b695911b-2973-11e6-acac-06b720391567","订单":{"订单类型":"1","付款类型":Γруз","cost":"350","description":","from":{"纬度":"55.15919993700593",3.3"},"至"期间":{"日期":"1464944075","从":"15","至":"18:30"},"发件人":{ИИваныч","phone":["7000009112"]},"cargo":[{"name":"wwww","description":"wwww","size":{"height":"2","width":"3","length":"4"},"loaders_count":"1","doe_need_packaging":false}]}}

我不是Rest和httprequest的专业人员,所以我不知道问题出在哪里,并且在第一个put方法中仍然有一个异常:

{"name":"Bad Request","message":"No API令牌已找到","代码":0,"状态":400,"类型":"yii\web\HttpException"}

如果有人有想法,请帮帮我!

试试这个,而不是

String str = String.valueOf(params);

我认为应该是

String str = String.valueOf(jsonObject);

因为您要将令牌添加到jsonObject,而不是params。

最新更新