Android:如何显示JSON Post结果

时间:2012-03-02 17:08:37

标签: android json toast

我无法从JSON获取显示结果。

Userfunctions.java
获取id,然后将其发布到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();
    }
}

如果ID为= 1

,则来自imageDisplay.php的JSON
{"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"}]}

如何制作TOAST以显示“imgPath”的结果?

三江源。

1 个答案:

答案 0 :(得分:1)

我不知道logcat是如何显示此信息的

无论如何这里是解决方案

  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();     
  }

我认为你只有这些数据,如果你有更多,那么你必须使用循环。