我正在尝试使用 Volley 执行 POST 请求,下面是我想要发出的请求类型、标头和 JSON 正文
我试过使用 Unirest api (Unirest.io) 但它需要 min sdk 为 24,所以我不得不切换到 Volley
HttpResponse<String> response = Unirest.post("https://api.msg91.com/api/v2/sendsms?country=91")
.header("authkey", "")
.header("content-type", "application/json")
.body("{ \"sender\": \"SOCKET\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"Message1\", \"to\": [ \"98260XXXXX\", \"98261XXXXX\" ] }, { \"message\": \"Message2\", \"to\": [ \"98260XXXXX\", \"98261XXXXX\" ] } ] }")
.asString();
用 OkHttp 试试,下面是一个简单的
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(JSON, json /* your json string*/);
Request request = new Request.Builder()
.url("https://api.msg91.com/api/v2/sendsms?country=91")
.post(body)
.addHeader("authkey", "Token")
.addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody=response.body().string()
}
您可以添加任何标题,将您的 json 字符串添加为正文
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句