@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();
}
任何人都可以帮助我。这是我的代码,我从服务器通过页面获取数据,如点击下一个按钮上的数字传递。当它不是从服务器提供数据时如何显示消息没有数据.. < / p>
答案 0 :(得分: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);
答案 1 :(得分: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();
}
答案 2 :(得分: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..
}