2016-06-07 85 views
0

我在解析和發送請求時遇到了很大的麻煩。 在請求正文中,我必須發送一個令牌,我已經成功完成驗證。 這裏有一個文檔:Http PUT在body body中:錯誤的請求錯誤,無法解析API令牌

PUT/API/VER1 /訂單{ 「令牌」:字符串, 「命令」:對象}

這裏就是我如何解析和獲取的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 + "!"); 

另一變型,而我USI ng 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」, 「命令」:{ 「order_type」:「1」,「payment_type」:「2」,「name」:「Какой-то груз」,「cost」:「350」,「description」:「」,「from」:{「latitude 「:」 55.15919993700593" , 「經度 」 「從1464944049」: 「65.15919993700593」}, 「fromPeriod」:{ 「日期」,「」: 「15」, 「爲」: 「18」} 「以」:{ 「city」:「Челябинск」,「address」:「Ленина, 3, 3」},「toPeriod」:{「date」:「1464944075」,「from」:「15」,「to」:「18 :30 「},」 發送者「: {「name」:「Попов」,「comment」:「Попов」,「phone」:[「+ 70000009111」]},「recipient」:{「name」:「Иванов ИванИваныч」,「comment」 ИвановИван Иваныч「,」phone「:[」70000009112「]},」cargo「:[{」name「:」wwww「,」description「:」wwww「,」size「:{」height「:」2 」, 「寬度」: 「3」, 「長度」: 「4」}, 「loaders_count」: 「1」, 「does_need_pa​​ckaging」:假}]}}

我不是在休息一個專業和HttpRequest的,所以我有不知道是什麼問題,而且還有了第一put方法例外:

{「名」:「錯誤的請求」,「消息」:「沒有API令牌 發現「」合作de「:0,」status「:400,」type「:」yii \ web \ HttpException「}

如果有人有想法幫助我!

+0

你能想出一個最小的例子嗎? – fmarc

+0

@fmarc,你是什麼意思? –

+0

我的意思是一個小例子。有人試圖回答您的問題,如果您的示例代碼較小,則會更容易理解問題。 – fmarc

回答

1

試試這個,而不是

String str = String.valueOf(params); 

我覺得應該是

String str = String.valueOf(jsonObject); 

因爲要添加的令牌的JSONObject和不PARAMS。

+0

它解決了你的問題嗎? – Ravikumar

+0

打印您的jsonObject並查看您發佈的json中是否存在「token」。 – Ravikumar

+0

幾件事情 1 - 是否應將令牌值作爲字符串傳遞? 2 - 你確定只有它的「令牌」而不是「訪問令牌」或別的嗎? 3-嘗試設置'httpPut.addHeader(「Accept」,「application/json」);' 我無法幫助您瞭解有關api的有限信息。 – Ravikumar

相關問題