0

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:

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();
}
  • Update your post with an example of what the php page returns. – JClaspill Dec 19 '13 at 00:31
  • Your answer is really here: [link](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Leonardo Dec 19 '13 at 00:32
  • thanks for the link , can you tell me how can i define the url ? – user2962529 Dec 19 '13 at 00:51
  • more like I think that you're not getting a JSON formatted response, instead it's just a normal array. if you want to parse that array using json parser. convert the result into JSON formatted string first. – KaHeL Dec 19 '13 at 02:49

1 Answers1

0

in your php, use

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

then in your android class,

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();
}
Rick Royd Aban
  • 904
  • 6
  • 33
  • Thanks my friend but it didn't work , Description Resource Path Location Type Type mismatch: cannot convert from JSONObject to JSONArray , – user2962529 Dec 20 '13 at 09:11