简体   繁体   English

在PHP中使用JSON将数据发送到Android

[英]Using JSON in PHP to send data to Android

I'm not sure that the JSON codes i have used in my PHP is right to send the data to my Android App. 我不确定我在PHP中使用的JSON代码是否适合将数据发送到我的Android应用。 My Android app works fine without any errors. 我的Android应用正常运行,没有任何错误。 I'm using Async Task to retrieve information from the server using JSON. 我正在使用异步任务使用JSON从服务器检索信息。

This is the code i have used in my Android app to get information from server : 这是我在Android应用程序中用来从服务器获取信息的代码:

JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
myoutput = jObj.getString("stringpval");

I havn't displayed the entire code, but i'm sure that all the other codes work fine. 我没有显示完整的代码,但是我确定所有其他代码都可以正常工作。

And below is my PHP script used to create a JSON array in PHP: 下面是我的PHP脚本,用于在PHP中创建JSON数组:

$a = array(
array('stringpval' => "My Value"));
print $json = json_encode($a);

So i have finished encoding json. 所以我已经完成了对json的编码。 I want my TextView in android to display "My Value"! 我希望Android中的TextView显示“我的价值”! Now when i run my android app and try to get the information from the server, my TextView goes blank. 现在,当我运行我的Android应用程序并尝试从服务器获取信息时,我的TextView变为空白。 It is Null! 是空的! What's the problem? 有什么问题? Any Solution? 有什么办法吗? Are the above codes correctly used? 以上代码是否正确使用?

The reason is that json_encode($a); 原因是json_encode($ a); is actually returning a JSONObject and not just a JSONArray as you're thinking. 实际上返回的是JSONObject,而不仅仅是您正在考虑的JSONArray。

What you're currently printing will look like this: 您当前正在打印的内容将如下所示:

{
    {
        'stringpval' = "My Value"
    }
}

I'm not sure why you're using a double-array for passing back a single value like that, so for now I would change your PHP file to: 我不确定为什么要使用双数组来传递这样的单个值,所以现在我将您的PHP文件更改为:

$a = array('stringpval' => "My Value");
print $json = json_encode($a);

Which will return: 将返回:

{
    'stringpval' = "My Value"
}

Which you could then access in Java by: 然后可以通过以下方式用Java访问:

JSONObject jObj = new JSONObject(result);
myoutput = jObj.getString("stringpval");

json usually is in two forms, either objects, or arrays, this is very important because you have to know which values are you retrieving from the server(json array or json object?) and hence parse them in the correct manner in the client side. json通常采用两种形式,即对象或数组,这非常重要,因为您必须知道要从服务器中检索哪些值(json数组还是json对象?),然后在客户端以正确的方式解析它们。 JsonArray are usually like this { "jsonarray_example": [ { "id": "1", "name": "android", "description": "android 4.4.3 kitkat", "updated_at": "0000-00-00 00:00:00" } , { "id": "2", "name": "android", "description": "android 4.1 jellybin", "updated_at": "0000-00-00 00:00:00" } ] } The opening and closing [] marks the array while the {} brackets marks the objects of the array called jsonarray_example but the brackets need not be like that now if your php page outputs something like that then you can retrieve the array else you have to retrieve the objects JsonObject in android so from your problem above you are trying to retrieve jsonObjects while your response is array you should use something like JsonArray result = new JsonArray() ; JsonObject obj = //access the server files result = obj.getJsonArray("stringpval") ; // proceed extracting individual content from the response JsonArray通常是这样的{ "jsonarray_example": [ { "id": "1", "name": "android", "description": "android 4.4.3 kitkat", "updated_at": "0000-00-00 00:00:00" } , { "id": "2", "name": "android", "description": "android 4.1 jellybin", "updated_at": "0000-00-00 00:00:00" } ] } []开头和结尾[]标记了数组,而{}括号则标记了名为jsonarray_example的数组的对象,但是如果您的php页面输出了类似的内容,则括号不必像现在这样,则可以检索其他数组您必须在android中检索对象JsonObject,因此从上述问题中尝试在响应为数组时尝试检索jsonObjects,则应使用类似JsonArray result = new JsonArray() ; JsonObject obj = //access the server files result = obj.getJsonArray("stringpval") ; // proceed extracting individual content from the response JsonArray result = new JsonArray() ; JsonObject obj = //access the server files result = obj.getJsonArray("stringpval") ; // proceed extracting individual content from the response

for more check this LINK and this ONE can be really helpful 更多的检查这个LINK ,这ONE可以真正的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM