リクエストの解析と送信で大きな問題が発生しました。リクエストの本文で、認証中に既に正常に取得したトークンを送信する必要があります。ドキュメントは次のとおりです。
PUT /api/ver1/orders {"token" : 文字列、"order" : オブジェクト}
そして、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"},"送信者":{"name":"Попов","comment":"Попов","phone":["+70000009111"]},"受取人":{"name":"Иванов Иван Иваныч","comment":"Иванов Иван Иваныч","phone":["70000009112"]},"cargo" :[{"名前":"wwww","説明":"wwww","サイズ":{"高さ":"2","幅":"3","長さ":"4"}," loaders_count":"1","does_need_packaging":false}]}}"width":"3","length":"4"},"loaders_count":"1","does_need_packaging":false}]}}"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"}
アイデアがある人は助けてください!