Android OKHTTP 字符串实体
Android OKHTTP StringEntity
我正在使用 OKHTTP 向 Microsoft Text Analytics API 发送 POST 请求,但无法在 OKHTTP 的请求正文中找出 StringEntity 的对应项。
我要发送的 HTTP 请求是:
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"
}
]
}
我现在的写法是:
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();
但是,这是不正确的。如何格式化我的请求正文,使其成为 "documents":[
{
"id": "string",
"text": "example string"
}
]?
尝试以下操作:
JSONObject document = new JSONObject();
document.put("id", "string");
document.put("text", "example string");
JSONArray documents = new JSONArray();
documents.put(document);
JSONObject root = new JSONObject();
root.put("documents", documents);
我正在使用 OKHTTP 向 Microsoft Text Analytics API 发送 POST 请求,但无法在 OKHTTP 的请求正文中找出 StringEntity 的对应项。
我要发送的 HTTP 请求是:
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"
}
]
}
我现在的写法是:
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();
但是,这是不正确的。如何格式化我的请求正文,使其成为 "documents":[ { "id": "string", "text": "example string" } ]?
尝试以下操作:
JSONObject document = new JSONObject();
document.put("id", "string");
document.put("text", "example string");
JSONArray documents = new JSONArray();
documents.put(document);
JSONObject root = new JSONObject();
root.put("documents", documents);