POST 要求 Header 和 JSON body
POST Request in Volley with Header and JSON body
我正在尝试使用 Volley 发出 POST 请求,下面是我想要发出的请求类型,一个 header 和一个 JSON body
我试过使用 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()
}
您可以添加任何 header,将您的 json 字符串添加为 body
我正在尝试使用 Volley 发出 POST 请求,下面是我想要发出的请求类型,一个 header 和一个 JSON body
我试过使用 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()
}
您可以添加任何 header,将您的 json 字符串添加为 body