Eric
Eric

Reputation: 1557

Android: How to display JSON Post result

I am having trouble to get display result from JSON.

Userfunctions.java
To get the id and then post it into PHP.

private static String imageURL = "http://xxx/imageDisplay.php";
public JSONObject imageDisplay(String id){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("id", id));
    JSONObject json = jsonParser.getJSONFromUrl(imageURL, params);
    return json;
}

PictureActivity.java

import org.example.veniew.library.UserFunctions; 
public class PictureActivity extends Activity {

private static final String TAG_ID = "ID";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.grid_layout);

    Intent picAct = getIntent();
    //get ID from previos activity
    String id = picAct.getStringExtra(TAG_ID);

    UserFunctions userFunction = new UserFunctions();
    JSONObject json = userFunction.imageDisplay(id);

    try {
        JSONObject jsonResponse = new JSONObject(new String());
        JSONArray imagelink = jsonResponse.getJSONArray("imagelink");

        String link = imagelink.toString();
        Toast.makeText(this, link, Toast.LENGTH_LONG).show();
        //How to TOAST imgPath?

    }catch (JSONException e) {
        e.printStackTrace();
    }
}

JSON from imageDisplay.php if ID is = 1

{"imgDisplay":[{"imgID":"1","imgPath":"imgFolder\/Penguins.jpg","ID":"1"},{"imgID":"2","imgPath":"imgFolder\/Jellyfish.jpg","ID":"1"},{"imgID":"4","imgPath":"imgFolder\/Koala.jpg","ID":"1"}]}

How do i make a TOAST to display the result of just "imgPath"??

Thankyou.

Upvotes: 1

Views: 1304

Answers (1)

Akram
Akram

Reputation: 7526

i dont know how the logcat is showing this info

anyway here is the solution

  JSONObject json = userFunction.imageDisplay(id);
  try { 


     JSONArray imagelink = json.getJSONArray("imgDisplay");

     String firstlink=imagelink.getJSONObject(0).getString("imgPath");

     String secondlink=imagelink.getJSONObject(1).getString("imgPath"); 


    String link = imagelink.toString();
     Toast.makeText(this, firstlink, Toast.LENGTH_LONG).show();
     //How to TOAST imgPath? 
  }catch (JSONException e) {
     e.printStackTrace();     
  }

i considered you have just this data if you have more then you have to use loop.

Upvotes: 1

Related Questions