0

How to return data from database in json via webpage. I saw some pages has .json extention or something like that. Is there some tool that returns it like that?

Previously I checked few tutorials where data was returned via php script and readed using java code. But for me it didn't worked, java code would read html code not the data that I would like to transfer.

try{
        jArray = new JSONArray(result);
        JSONObject json_data= null;
        for(int i=0;i<jArray.length();i++){
               json_data = jArray.getJSONObject(i);
               ct_id=json_data.getInt("CITY_ID");
               System.out.println(ct_id);
               ct_name=json_data.getString("CITY_NAME");
               System.out.println(ct_name);
           }
        }
        catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
        }

Exception would always show up. So I printed lines that it gets and it html code. How to fix it?

Shien
  • 107
  • 4
  • 12
  • 1
    What exception is showing up? What is the JSON data you are trying to parse? – Danny Mar 07 '12 at 19:18
  • http://stackoverflow.com/questions/9441932/android-json-error-expected-begin-object-but-was-begin-array-at-line-1-column-2 – Yaqub Ahmad Mar 07 '12 at 19:18
  • You need to figure out how you're going to send json from your web service, my favorite way is to return json if they send a request header like: Content-type: application/json. then see my answer here: http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android/9606629#9606629 – bbedward Mar 07 '12 at 19:19
  • exception is that "No City Found" – Shien Mar 07 '12 at 19:22
  • I was using this php code to read data 1, "NAME" => "Vilnius"); print(json_encode($array)); mysql_close(); ?> – Shien Mar 07 '12 at 19:31
  • {"ID":1,"NAME":"Vilnius"} this is what php script returns – Shien Mar 07 '12 at 19:37

0 Answers0