How do I retrieve json array data and view it in the text viewer?

advertisements

I've been using the coding from an example from this link: http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/ The code is as shown below:

     public class food extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String result = null;
        InputStream is = null;
        StringBuilder sb=null;
        String result=null;
            TextView fdi = (TextView)findViewById(R.id.textView1);
            TextView fdn = (TextView)findViewById(R.id.textView2);

        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://127.0.0.1/food.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
        }

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result=sb.toString();

        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        //paring data
        int fd_id;
        String fd_name;
        try{
        jArray = new JSONArray(result);
        JSONObject json_data=null;

        for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
                fd_id=json_data.getInt("FOOD_ID");
                fd_name=json_data.getString("FOOD_NAME");
        }

        }catch(JSONException e1){
            Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();
        }catch (ParseException e1){
            e1.printStackTrace();
        }
    }
}

Now my question is how to pass this data to a list view, like for each iteration a list item must be added with the retrieved data.

Please help me

Thanks in advance


You are already getting strings in below code, Why don't you use that.

         for(int i=0;i<jArray.length();i++){
            json_data = jArray.getJSONObject(i);
            fd_id=json_data.getInt("FOOD_ID");
            fd_name=json_data.getString("FOOD_NAME");
          }

//fdname and fd_id you are getting it right,

If your code is not working then add what problem you are facing.

according to your response/result from the server below code is working for me and giving output in string ,

                try {
          String response ="[{\"FOOD_ID\":\"1\",\"FOOD_NAME\":\"Rice\"},{\"FOOD_ID\":\"2\",\"FOOD_NAME\":\"Daal\"}] ";
           JSONArray array;

            array = new JSONArray(response);

           for (int i = 0; i < array.length(); i++) {
             JSONObject obj = array.getJSONObject(0);
             String id_fd = obj.getString("FOOD_ID");
             String name_fd = obj.getString("FOOD_NAME");
             Log.d("JSONArray", id_fd+"   " +name_fd);
        }} catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

please use it according to your need.