1
我正在使用okhttp lib與Java和PHP。我的Java客戶端正在運行以下代碼。JSON通過okhttp與Java和PHP請求
public class Connection {
public static final MediaType JSON = MediaType
.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) throws IOException {
Connection example = new Connection();
String json = "{'input':'test'}";
String response = example.post("http://localhost/android_api/index.php", json);
System.out.println(response);
}
}
在服務器端,我嘗試用下面的代碼解碼JSON字符串,但是我的webservice只返回一個NULL。
<?php
$rawData = file_get_contents("php://input");
$json = json_decode($rawData);
var_dump($json);
?>
我是什麼錯誤?
好吧,我發現我的JSON字符串是錯誤的。我發送了一個錯誤的String {'input':'test'}而不是{「input」:「test」}但是我不能在字符串中使用「我怎樣才能解決問題而不使用雙引號字符, 「? – Aleksei 2015-03-25 13:03:23