1
@Override
protected String doInBackground(String... params) {
        try{
            JSONParser jParser = new JSONParser();
            String url="http://twominenglish.com/api/getlatest?";
             IdArray = jParser.getJSONFromUrl(url+"page="+pages);

            mLatestList.clear();
            for(int i=0;i<IdArray.length();i++){
                if(IdArray.isNull(i))
                {
                    Toast.makeText(mContext, "No Value", Toast.LENGTH_SHORT).show();
                }
                try{
                     JSONObject jObject;
                     mLtest=new Latest();
                     jObject=IdArray.getJSONObject(i);

                     mLtest.SetID(jObject.getString("ID"));
                     //mLtest.SetImageUrl(jObject.getString("ImageURL"));
                     String path="http://twominenglish.com"+jObject.getString("ImageURL");
                     mLtest.SetImageUrl(path);
                     mLtest.SetDescription(jObject.getString("Description"));
                     mLtest.SetTitle(jObject.getString("Title"));
                     mLatestList.add(mLtest);
                     mLtest=new Latest();

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

任何人都可以帮助我。这是我的代码,我通过页面从服务器获取数据,例如在单击的下一个按钮上传递数字。当它不从服务器获取数据时,我如何显示消息没有数据..

4

3 回答 3

1

请在您的网址中提供 json 数组名称

喜欢 -

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        .
        .
        .
        .
  ]
}

jParser.getJSONFromUrl(url+"page="+pages)函数返回一个 json 对象。

编写以下代码

// contacts JSONArray
JSONArray IdArray = null;

/ getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
    // Getting Array of Contacts
    IdArray = json.getJSONArray(TAG_CONTACTS);
于 2013-07-31T07:40:54.630 回答
0

正如我在您的代码中看到的,您的 JSONArray 是

IdArray = jParser.getJSONFromUrl(url+"page="+pages);

现在,在那条线之后,检查它的长度

if(IdArray.length()>0)
{
// do all operations here
}
else{
Toast.makeText(mContext, "Array empty", Toast.LENGTH_SHORT).show();
}
于 2013-07-31T07:41:52.350 回答
0

这样做...这是一个异步类...

类 MyTask 扩展 AsyncTask {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }


    @Override
    protected String doInBackground(String... params) {

        String result=IOUtils.getUrlResponse(params[0]);

        return result;

        // TODO Auto-generated method stub

    }
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
            JSONArray mainJson = new JSONArray(result);
            JSONArray objjson=mainJson.getJSONObject(0).getJSONArray("data");

     Log.i("lenghth",objjson.length());  //this will return length of Json Array..

}
于 2013-07-31T07:42:41.807 回答