J'ai un gros problème pour analyser et envoyer une requête. Dans le corps de la requête, je dois envoyer un jeton que j'ai déjà réussi pendant l'authentification. Voici un doc:Http PUT dans le corps du corps: Erreur de requête incorrecte, impossible d'analyser le jeton API
PUT/api/ver1/commandes { "jeton": String, "ordre": Object}
Et voici comment je l'analyse syntaxique et d'obtenir 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());
Et voici Httpput demande
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 + "!");
Une autre variante est alors que je suis usi ng com.loopj.android.http.AsyncHttpClient. Je ne pas un autre obtenir, corriger et poster demandes et tout fonctionnait avec succès sauf PUT. Voici le code que j'ai:
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);
}
voici entuty/JSONObject chaîne:
{ "jeton": "b695911b-2973-11e6-acac-06b720391567", "ordre": { "order_type": "1", "payment_type": "2", "name": "Какой-то груз", "coût": "350", "description": "", "de": {"latitude ":" 55.15919993700593 "," longitude ":" 65.15919993700593 "}," fromPeriod ": {" date ":" 1464944049 "," de ":" 15 "," à ":" 18 "}," à ": { "ville": "Челябинск", "adresse": "Ленина, 3, 3"}, "toPeriod": {"date": "1464944075", "de": "15", "à": "18 : 30 "}," expéditeur ": {"name": "Попов", "comment": "Попов", "téléphone": ["+ 70000009111"]}, "destinataire": {"name": "Иванов Иван Иваныч", "comment": " Иванов Иван Иваныч "," phone ": [" 70000009112 "]}," cargo ": [{" name ":" wwww "," description ":" wwww "," taille ": {" height ":" 2 », "largeur": "3", "longueur": "4"}, "loaders_count": "1", "does_need_packaging": false}]}}
Je ne suis pas un professionnel de repos et HTTPRequest donc j'ai aucune idée quel est le problème et ont encore eu une exception dans la première méthode de vente:
{ « name »: « Bad Request », « message »: "aucune API jeton trouvé "," co de ": 0," état ": 400," type ":" yu \ web \ HttpException "}
S'il vous plaît, si quelqu'un a une idée me aider!
Pourriez-vous trouver un exemple minimal? – fmarc
@fmarc, quel exemple voulez-vous dire? –
Je veux dire un exemple plus petit. Quelqu'un essayant de répondre à votre question trouvera plus facile de comprendre le problème si votre code d'exemple est plus petit. – fmarc