java - 正文中的 Http PUT : Bad request error, 无法解析 API token

标签 java json parsing http-put frombodyattribute

我在解析和发送请求时遇到了很大的麻烦。 在请求正文中,我必须发送一个在身份验证时已成功获得的 token 。 这是一个文档:

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字符串:

{"token":"b695911b-2973-11e6-acac-06b720391567","order":{"order_type":"1","payment_type":"2","name":"Какой-то груз","cost":"350","description":"","from":{"latitude":"55.15919993700593","longitude":"65.15919993700593"},"fromPeriod":{"date":"1464944049","from":"15","to":"18"},"to":{"city":"Челябинск","address":"Ленина, 3, 3"},"toPeriod":{"date":"1464944075","from":"15","to":"18:30"},"sender":{"name":"Попов","comment":"Попов","phone":["+70000009111"]},"recipient":{"name":"Иванов Иван Иваныч","comment":"Иванов Иван Иваныч","phone":["70000009112"]},"cargo":[{"name":"wwww","description":"wwww","size":{"height":"2","width":"3","length":"4"},"loaders_count":"1","does_need_packaging":false}]}}

我不是 Rest 和 httprequest 方面的专业人士,所以我不知道问题是什么,并且在第一个 put 方法中仍然遇到异常:

{"name":"Bad Request","message":"No API token found","code":0,"status":400,"type":"yii\web\HttpException"}

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

最佳答案

试试这个,而不是

String str = String.valueOf(params);

我觉得应该是

String str = String.valueOf(jsonObject);

因为您将 token 添加到 jsonObject 而不是 params。

关于java - 正文中的 Http PUT : Bad request error, 无法解析 API token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37674283/

相关文章:

javascript - OpenWeatherMap API : Uncaught TypeError - Cannot read property 'temp' of undefined(. ..)

python - 解析存储在 pandas 数据框列中的推文

java - 以编程方式调用 JSP 解析器

java - 无法在jenkins中执行java程序?

java - 长时间不活动后tomcat没有响应

java - android 如何以及何时使用 getSupportFragmentManager (新手很困惑)

java - 列出java中所有毕达哥拉斯三元组

javascript - google电子表格导出为json文件,如何格式化?

json - 从 object/json 中删除字段属性

java - 如何使用 antlrv3 生成的词法分析器和解析器提供语法高亮显示?