简体   繁体   English

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

[英]Http PUT in body body: Bad request error, can't parse API token

I've got a big trouble parsing and sending request. 我在解析和发送请求时遇到了很大的麻烦。 In body of request I have to send a token that I've already got successfully while authetification. 在请求正文中,我必须发送在身份验证时已经成功获取的令牌。 Here's a doc: 这是一个文档:

PUT /api/ver1/orders {"token" : String, "order" : Object} PUT / api / ver1 / orders {“ token”:字符串,“ order”:对象}

And here's how I parsing and getting JSONObject: 这是我解析和获取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());

And here's Httpput request 这是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 + "!");

Another variant is while I'm using com.loopj.android.http.AsyncHttpClient. 另一个变体是当我使用com.loopj.android.http.AsyncHttpClient时。 I don't another get, patch and post requests and everything was successfully working except PUT . 我没有另一个get,patch和post请求,除了PUT之外,其他所有东西都成功运行。 Here's the code I've got: 这是我得到的代码:

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);
}

here's entuty/jsonObject String: 这是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}]}} {“ token”:“ b695911b-2973-11e6-acac-06b720391567”,“ order”:{“ order_type”:“ 1”,“ payment_type”:“ 2”,“ name”:“Какой-тогруз”,“ cost“:” 350“,” description“:”“,”来自“:{” latitude“:” 55.15919993700593“,” longitude“:” 65.15919993700593“},” fromPeriod“:{” date“:” 1464944049“,” from“:” 15“,” to“:” 18“},” to“:{” city“:”Челябинск“,” address“:”Ленина,3,3“},” toPeriod“:{” date“ :“ 1464944075”,“从”:“ 15”,“到”:“ 18:30”},“发送者”:{“名称”:“Попов”,“评论”:“Попов”,“电话”:[ “ +70000009111”]},“收件人”:{“名称”:“ИвановИванИваныч”,“评论”:“ИвановИванИваныч”,“电话”:[“ 70000009112”]},“货物”:[{ name“:” wwww“,” description“:” wwww“,” size“:{” height“:” 2“,” width“:” 3“,” length“:” 4“},” loaders_count“:” 1“,” does_need_pa​​ckaging“:false}]}}

I'm not a professional in Rest and httprequest so I've got no idea what the problem is and still have got an exception in the first put method: 我不是Rest和httprequest的专家,所以我不知道问题出在什么地方,但第一个put方法仍然有异常:

{"name":"Bad Request","message":"No API token found","code":0,"status":400,"type":"yii\\web\\HttpException"} {“名称”:“错误请求”,“消息”:“未找到API令牌”,“代码”:0,“状态”:400,“类型”:“ yii \\ web \\ HttpException”}

Please if anyone has an idea help me! 如果有人有想法请帮助我!

Try this, instead of 试试这个,而不是

String str = String.valueOf(params);

I think it should be 我认为应该

String str = String.valueOf(jsonObject);

Because you are adding your token to jsonObject and not params. 因为您是将令牌添加到jsonObject而不是params中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM