异常来自Android的Java和PHP REST API JSON解析 [英] Exception parsing JSON from Android Java and PHP REST API
问题描述
我想送一个JSON是这样的:
I want to send a JSON like this:
{"reverseme" : "Reverse Me!" }
因此,我创建这个Java:
So I created this Java:
JSONObject jsonToSend = new JSONObject()
.put("reverseme", "Reverse ME!")
如果我将其转换为字符串,并将其发送给我的API我没有问题:
If I transform it to String and send it to my api I have no problems:
HttpClient client = new HttpClient();
HttpPost post = new HttpPost("http://192.168.0.10/api");
List<NameValuePair> postData= new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("json", jsonToSend.toString()));
post.setEntity(new UrlEncodedFormEntity(postData));
有它会去我的PHP API是如此简单(我测试一些事情):
There it will go to my PHP API is as simple as this (i'm testing some things):
<?php
$JSON = json_decode($_POST['json'], true);
print(
json_encode( Array(
'reversed' : strrev($JSON['reverseme'])
))
); ?>
最后,Java的code来读取响应是这样的:
Finally, Java Code to read the response is this:
HttpResponse httpresponse = client.execute(post);
String jsonstringresponse = EntityUtils.toString(httpresponse.getEntity());
JSONObject jsonresponse = new JSONObject(jsonstringresponse);
这使我这个堆栈跟踪:
It gives me this StackTrace:
org.json.JSONException: A JSONObject text must begin with '{' at character 1 of "{\"reversed\": "!EM esreveR"}"
at org.json.JSONTokener.syntaxError(JSONTokener.java:448)
at org.json.JSONObject.<init>(JSONObject.java:178)
at org.json.JSONObject.<init>(JSONObject.java:246)
-> at com.example.test.MainActivity.onClick(MainActivity.java:67)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at android.view.View$1.onClick(View.java:2026)
...
当然我,因为你可以在堆栈跟踪的最后一行看到我有一些,从阅读的EditText,把JSONresult到一个TextView的Android UI。
Of course I, as you can see on StackTrace's last line I have some Android UI that read from a EditText and put JSONresult to a TextView.
的问题是,从服务器说到:
The problem is that from server it comes:
"{\"reversed\": "!EM esreveR"}"
这里有什么问题吗?为什么json_en code转义每个字符?难道是字符集问题,或者一些需要清理的还是什么?
What is the problem here? Why does json_encode escape every character? Is it Charset problem or some need of cleaning or what?
在感谢任何意见。
更新1
在Apache的错误日志,我发现这行:
UPDATE 1 On Apache error log I found this line:
[error] PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in /var/www/controllers/api.inc.php on line 4.
我也曾想过,这可能是连PHP错误配置?
I have thought that it could be even a PHP misconfiguration?
更新2
json_en code正在接受一家西班牙的字符串,这是该错误的原因是:读Atención给出,因为O
json_encode was receiving a Spanish String, that's the reason of that error: Reading "Atención" gives error because of 'ó'.
我暂时使用stripslashes()函数和删除第一和最后一个字符,使其工作。
I'm temporally using stripslashes() and deleting first and last character to make it work.
推荐答案
当时服务器配置错误,而且PHP被保存在ISO-8859-1字符集让PHP搞砸3种不同的字符集。
Was a Server configuration fault and that the PHP was saved on ISO-8859-1 charset so PHP messed with 3 different charsets.
这篇关于异常来自Android的Java和PHP REST API JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!