本文介绍了在解析JSON数据的android空响应即将到来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
###这是在整个应用程序的主要方法取决于
私人无效setOnListener(){
// TODO自动生成方法存根
btnGetData.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
新JSONParsing()执行(); } }); btnStoreData.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
///的System.out.println(价值观:+ cursor.getColumnCount());
光标= newrummydb.query(DATABASE_TABLE,新的String [] {
cityid,城市名,createddate,isactive,STATEID,
Statename的},NULL,NULL,NULL,NULL,NULL); getfromdatabase();
}
}); } 保护无效getfromdatabase()
{ ///从数据库获取数据 如果(cursor.moveToFirst())
{
做
{ tvCity_ID.setText(cursor.getString(0));
tvCityName.setText(cursor.getString(1));
tvCreatedDate.setText(cursor.getString(2));
tvIsActive.setText(cursor.getString(3));
tvState_ID.setText(cursor.getString(4));
tvStateName.setText(cursor.getString(5));
tvUpdatedDate.setText(cursor.getString(6));
}
而(cursor.moveToNext());
}
mydb.close();
} 保护类JSONParsing扩展的AsyncTask<字符串,字符串,JSONObject的> {
私人ProgressDialog progressdialog; @覆盖
在preExecute保护无效(){
super.on preExecute();
progressdialog =新ProgressDialog(NewRummyActivity.this); progressdialog.setMessage(检索数据...);
progressdialog.setCancelable(真);
progressdialog.show(); } @覆盖
受保护的JSONObject doInBackground(字符串... PARAMS){
// TODO自动生成方法存根
RummyJSONParser jsonparsing =新RummyJSONParser();
JSONObject的newrummy = jsonparsing.getJSONFromURL(URL); 返回newrummy;
} @覆盖
保护无效onPostExecute(JSONObject的newrummy){
progressdialog.dismiss(); 尝试{ ***
// data.set_ID(Integer.parseInt(newrummy.getString(DFID)));
values.put("_id", i + 1);
cityid = newrummy.getJSONArray(cityid).getString(i);
values.put("city", cityid);
cityname = newrummy.getJSONArray(cityname).getString(i);
values.put("cityname", cityname);
createddate = newrummy.getJSONArray(createddate).getString(i);
values.put("cityname", createddate);
isactive = newrummy.getJSONArray(isactive).getString(i);
values.put("ground", isactive);
stateid = newrummy.getJSONArray(stateid).getString(i);
values.put("stateid", stateid);
statename = newrummy.getJSONArray(statename).getString(i);
values.put("statename", statename);
updateddate = newrummy.getJSONArray(updateddate).getString(i);
values.put("updateddate", updateddate);
newrummydb.insert(DATABASE_TABLE, null, values);
}
final CustomAdapter adapter = new CustomAdapter(
NewRummyActivity.this, R.layout.newrummylistitem,
cursor);
newrummydatalist.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
My LOGCAT showing this kind of exception ::
04-15 19:06:36.713: I/System.out(28523): Values are:null
04-15 19:06:36.845: W/System.err(28523): org.json.JSONException: No value for cityMasterEntity
04-15 19:06:36.845: W/System.err(28523): at org.json.JSONObject.get(JSONObject.java:354)
04-15 19:06:36.845: W/System.err(28523): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
04-15 19:06:36.845: W/System.err(28523): at com.example.newrummyjson.NewRummyActivity$JSONParsing.onPostExecute(NewRummyActivity.java:168)
04-15 19:06:36.845: W/System.err(28523): at com.example.newrummyjson.NewRummyActivity$JSONParsing.onPostExecute(NewRummyActivity.java:1)
04-15 19:06:36.845: W/System.err(28523): at android.os.AsyncTask.finish(AsyncTask.java:631)
解决方案
json=sb.toString();
System.out.println("Values are:" +sb);
newrummy=new JSONObject(); //problem lies here
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newrummy;
the problem with your code you are returning an empty json object.
Change your code from,
newrummy=new JSONObject();
to newrummy=new JSONObject(json);
这篇关于在解析JSON数据的android空响应即将到来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!