I have been trying to send a POST request with some user data. The request seems incomplete. I need help with this line
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
StringEntity postingString = new StringEntity(gson.toJson(pj));
RequestBody body = RequestBody.create(mediaType, "{\n \"url\":\"https://okirizuri.herokuapp.com/check\",\"phone\":" + phone + "\"}");
Request request = new Request.Builder()
.url("https://okirizuri.herokuapp.com/lipa")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.build();
String phone = editText.getText().toString().trim();
RequestBody body = RequestBody.create(mediaType, "{\n \"url\":\"https://okirizuri.herokuapp.com/check\",\"phone\":" + phone + "\"}");
The phone is a variable which has a user number but its not showing at the endpoint
答案 0 :(得分:1)
在您的示例中,您似乎在\“ phone \”之后缺少了一个额外的双引号:这使其无效json。您已经在电话号码的右侧(而不是左侧)使用了引号。
RequestBody body = RequestBody.create(mediaType, "{\n \"url\":\"https://okirizuri.herokuapp.com/check\",\"phone\":" + phone + "\"}");
JSON:
{
"url": "https://okirizuri.herokuapp.com/check",
"phone": 555-555-5555"
}
应该是:
RequestBody body = RequestBody.create(mediaType, "{\n \"url\":\"https://okirizuri.herokuapp.com/check\",\"phone\":\"" + phone + "\"}");
JSON:
{
"url": "https://okirizuri.herokuapp.com/check",
"phone": "555-555-5555"
}