如何解析Android上的Array

时间:2022-05-07 21:43:35

I am trying to get array members from a PHP server to my places[] array. Actually, I have searched many topics but whenever I tried the codes it always crashed and my app always stopped unexpectedly. My json is ; Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 0 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 ).It can be a silly question but i dont know how can i write the url for my php data.it works on my browser like <"localhost:8080/Androidconnection/parse.php"> but in the code i think i should write something different to achieve this data , and what should it be ? and also here is the code that I'm trying to get the data:

我试图将数组成员从PHP服务器到我的places []数组。实际上,我搜索了很多主题,但每当我尝试代码时,它总是崩溃,我的应用程序总是意外停止。我的json是;数组([0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 0 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16 ] => 1 [17] => 1 [18] => 1 [19] => 1)。这可能是一个愚蠢的问题,但我不知道我怎么能为我的php数据写url.it在我的浏览器上工作喜欢<“localhost:8080 / Androidconnection / parse.php”>但在代码中我认为我应该写一些不同的东西来实现这些数据,它应该是什么?而且这里是我试图获取数据的代码:

String url="http://localhost:8080/Androidconnection/parse.php";
JSONParser jParser=new JSONParser();
JSONArray json=jParser.getJSONFromUrl(url);

try {
    for(int i=0;i<json.length();i++) {
        JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
        places[i]=c.getInt("id");
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
}

1 个解决方案

#1


0  

in your php, use

在你的PHP中,使用

return json_encode(yourArray);

also, you dont use localhost since it wont be accessible online, you have to create an online server yourself e.g. freetzi.com that is a freehosting site

另外,你不使用localhost,因为它无法在线访问,你必须自己创建一个在线服务器,例如freetzi.com这是一个免费主机网站

then in your android class,

然后在你的android类中,

JSONParser jParser=new JSONParser();
JSONArray json=jParser.getJSONFromUrl(yourUrl);

try {
    for(int i=0;i<json.length();i++) {
        JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
        places[i]=c.getInt("id");
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
}

#1


0  

in your php, use

在你的PHP中,使用

return json_encode(yourArray);

also, you dont use localhost since it wont be accessible online, you have to create an online server yourself e.g. freetzi.com that is a freehosting site

另外,你不使用localhost,因为它无法在线访问,你必须自己创建一个在线服务器,例如freetzi.com这是一个免费主机网站

then in your android class,

然后在你的android类中,

JSONParser jParser=new JSONParser();
JSONArray json=jParser.getJSONFromUrl(yourUrl);

try {
    for(int i=0;i<json.length();i++) {
        JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
        places[i]=c.getInt("id");
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
}