0
J'utilise OKHTTP pour envoyer des requêtes POST à l'API Microsoft Text Analytics, mais je ne trouve pas la contrepartie de StringEntity dans le corps de la requête d'OKHTTP.Android OKHTTP StringEntity
La requête HTTP que je veux envoyer est:
POST https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages?numberOfLanguagesToDetect=1 HTTP/1.1
Content-Type: application/json
Host: westus.api.cognitive.microsoft.com
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
{
"documents": [
{
"id": "string",
"text": "example string"
}
]
}
La façon dont je l'ai écrit est en ce moment:
OkHttpClient client = new OkHttpClient();
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/languages").newBuilder();
urlBuilder.addQueryParameter("numberOfLanguagesToDetect", "1");
String url = urlBuilder.build().toString();
JSONObject text = new JSONObject();
text.put("id", "string");
text.put("text", "example string");
MediaType jsonMT = MediaType.parse("application/json; charset=utf-8");
RequestBody rBody = RequestBody.create(jsonMT, text.toString());
Request request = new Request.Builder()
.header("Content-Type", "application/json")
.header("Ocp-Apim-Subscription-Key", sub-key)
.url(url)
.post(rBody)
.build();
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
Cependant, ce qui est faux. Comment puis-je formater mon corps de requête pour qu'il soit "documents": [ { "id": "chaîne", "texte": "exemple chaîne" } ]?
Merci! Ça fonctionne maintenant! –