1

I've been using the coding from an example from this link: 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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

now i want to pass this data to textview for example, but whn i pass data, i get null pointer exception error.. let me edit my question so that it will be much clear.
yes do it, and also print your result , so you can know what is coming from server,, is it really an JsonArray...
can u do one thing?? paste print your result, second thing in the error it must be showing a line number in which exception occuring, add that too.. then i will try it at my side..
yes, jArray value: [{"FOOD_ID":"1","FOOD_NAME":"Rice"},{"FOOD_ID":"2","FOOD_NAME":"Daal"}]
I edited my answer please check it out, i can get the strings from the result.
1

The root cause of the error a NullPointerException. This means that you are trying to use an object that has the value null. Usually this is because the object has not yet been initialised.

In your case the nullpointer is caused by something at line 29 of your Food class, probably one of these two lines

   TextView fdi = (TextView)findViewById(R.id.textView1);
   TextView fdn = (TextView)findViewById(R.id.textView2);

Are you sure that both the R.id.textView1 and R.id.textView1 exist? They should be specified somewhere in a layout xml file, something like this:

 <TextView
    android:id="@+id/textView1"
    ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.