###这是整个应用程序的主要方法取决于
private void setOnListener() {
// TODO Auto-generated method stub
btnGetData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new JSONParsing().execute();
}
});
btnStoreData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/// System.out.println("Values of: " + cursor.getColumnCount());
cursor = newrummydb.query(DATABASE_TABLE, new String[] {
cityid, cityname, createddate, isactive, stateid,
statename }, null,null,null,null,null);
getfromdatabase();
}
});
}
protected void getfromdatabase()
{
/// Getting Data from database
if(cursor.moveToFirst())
{
do
{
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));
}
while (cursor.moveToNext());
}
mydb.close();
}
protected class JSONParsing extends AsyncTask<String, String, JSONObject> {
private ProgressDialog progressdialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressdialog = new ProgressDialog(NewRummyActivity.this);
progressdialog.setMessage("Retrieving Data...");
progressdialog.setCancelable(true);
progressdialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
RummyJSONParser jsonparsing = new RummyJSONParser();
JSONObject newrummy = jsonparsing.getJSONFromURL(url);
return newrummy;
}
@Override
protected void onPostExecute(JSONObject newrummy) {
progressdialog.dismiss();
try {
***
citymaster = newrummy.getJSONArray(CITYARRAY); ///面对问题 这条线建议我做一些编辑 for(int i = 0; i&lt; citymaster.length(); i ++){
newrummy = citymaster.getJSONObject(i);
// 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();
}
}
}
}
我的LOGCAT显示了这种异常::
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)
答案 0 :(得分:1)
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;
你的代码问题是你返回一个空的json对象。
更改您的代码,
newrummy=new JSONObject();
至newrummy=new JSONObject(json);