Java에서 JSON 객체를 포함하는 HTMLPost 요청을 생성했으며이를 PHP에서 구문 분석하려고합니다. PHP에서 JSON POST 요청 구문 분석
public static String transferJSON(JSONObject j) {
HttpClient httpclient= new DefaultHttpClient();
HttpResponse response;
HttpPost httppost= new HttpPost(SERVERURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("json", j.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
}
그리고 서버 내가 JSON 특수 문자 인코딩되기 때문에이 추측
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// input = "json=%7B%22locations%22%3A%5B%7B%22..."
$input = file_get_contents('php://input');
// jsonObj is empty, not working
$jsonObj = json_decode($input, true);
에
.json_decode 반환 빈 응답
어떤 생각을 왜?
감사합니다 더 나은 솔루션을 입력 변환. 문자열이 3 개인 생성자가 없습니다. 'StringEntity stringEntity = new StringEntity (j.toString(), "UTF-8")를 사용했습니다. stringEntity.setContentType ("application/json"); ' –
사용중인 HTTP 클라이언트/HTTP 구성 요소의 버전과 확실히 다를 수 있습니다. 난 그냥 [최신 javadoc] (http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/StringEntity.html)에서 생성자를 뽑아 – Charlie
그래야한다. Android에서 자바 버전을 사용하고 있습니다. –