当我的 WEBSERVICE 没有返回数据时,我想添加一条 toast 消息并将用户返回到主要活动,并允许我解释更多。当用户搜索查询时,Web 服务将数据返回到我的 JSON 字符串,如下所示。
08-30 00:36:07.917: D/JSON String(1891): {
08-30 00:36:07.917: D/JSON String(1891): "all" : {
08-30 00:36:07.917: D/JSON String(1891): "count" : 25,
08-30 00:36:07.917: D/JSON String(1891): "questions" : [ {
08-30 00:36:07.917: D/JSON String(1891): "Id" : "20100728112033AAb4hTA",
08-30 00:36:07.917: D/JSON String(1891): "Subject" : "What is the oldest a bitch can more or less safely breed?",
08-30 00:36:07.917: D/JSON String(1891): "Content" : "Don't worry I'm not going to breed - both my bitches are getting neutered in the next year, as is my dog! Just that me and a friend were talking about it after talking to the man who gave me Misty (my border collie) who said she could still breed at 7 - I thought the oldest was 5?\n",
08-30 00:36:07.917: D/JSON String(1891): "Date" : "2010-07-28 18:20:33",
08-30 00:36:07.917: D/JSON String(1891): "Timestamp" : "1280341233",
08-30 00:36:07.917: D/JSON String(1891): "Link" : "http://answers.yahoo.com/question/?qid=20100728112033AAb4hTA",
08-30 00:36:07.917: D/JSON String(1891): "Type" : "Answered",
08-30 00:36:07.917: D/JSON String(1891): "CategoryId" : 396546021,
08-30 00:36:07.917: D/JSON String(1891): "CategoryName" : "Dogs",
08-30 00:36:07.917: D/JSON String(1891): "UserId" : "cP16Ctgxaa",
08-30 00:36:07.917: D/JSON String(1891): "UserNick" : "Kiko",
08-30 00:36:07.917: D/JSON String(1891): "UserPhotoURL" : "http://l.yimg.com/dg/users/1t1USxJpxAAEBQOGZjBMW0-5Wp_EG.medium.jpg",
08-30 00:36:07.917: D/JSON String(1891): "NumAnswers" : 8,
08-30 00:36:07.917: D/JSON String(1891): "NumComments" : 0,
08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswer" : "You just \"imagined\" that ,dear...not \"thought\". & your imagination is WRONG.\r\n\r\n9 or even 10,for a healthy TOP-PRODUCING bitc-h.\r\n\r\n\r\n\r\n*&* bitches are SPAYED & dogs are CASTRATED......big scary correct ADULT words.",
08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswererId" : "clN6YITvaa",
08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswererNick" : "Debunker",
08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswerTimestamp" : "1280317077",
08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswerAwardTimestamp" : "1280835336"
08-30 00:36:07.917: D/JSON String(1891): }, {
这是我如何解析这些数据以获取我想要的特定数据的代码
protected String doInBackground(String... args) {
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com";
JSONParsser jParser = new JSONParsser();
JSONObject json = jParser.readJSONFeed(URL);
try {
JSONArray questions = json.getJSONObject("all").getJSONArray("questions");
for(int i = 0; i < questions.length(); i++) {
JSONObject question = questions.getJSONObject(i);
String Subject = question.getString(TAG_QUESTION_SUBJECT);
String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = question.getString(TAG_QUESTION_CONTENT);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TAG_QUESTION ;
}
但是,当用户搜索查询并且 Web 服务没有返回任何结果并且没有结果时,我的意思是 Web 服务数据具有"count" : 0,
并且在我的 JSON 字符串中看起来像这样。
09-02 00:25:55.466: D/JSON String(882): {
09-02 00:25:55.466: D/JSON String(882): "all" : {
09-02 00:25:55.466: D/JSON String(882): "count" : 0,
09-02 00:25:55.466: D/JSON String(882): "questions" : [ ]
09-02 00:25:55.466: D/JSON String(882): }
09-02 00:25:55.466: D/JSON String(882): }
发生这种情况时,我希望弹出一条 toast 消息并将用户返回到主要活动,但我只是不知道当它返回计数 0 时如何解析该数据。我已经知道我需要if
在此位置添加一条语句
try {
// if statement here
JSONArray questions = json.getJSONObject("all").getJSONArray("questions");
for(int i = 0; i < questions.length(); i++) {
JSONObject question = questions.getJSONObject(i);
String Subject = question.getString(TAG_QUESTION_SUBJECT);
String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = question.getString(TAG_QUESTION_CONTENT);
我需要知道如何解析"count" : 0,
,以便显示 toast 消息,然后将用户返回到主要活动。希望你在这里看到我的想法,如果你有任何困难,请告诉我,试着理解我的意思。PS如果你想知道JSONParsser
是什么,它只是我用来连接到网络服务的类,searchTerm
我只是传递editText
和编码它。
@Override
protected void onPostExecute(String file_URL) {
if(file_URL.equals("0")) {
pDialog.dismiss();
Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show();
finish();
}else{
if (pDialog.isShowing()) pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
R.layout.listelements,
new String[] { TAG_QUESTION_SUBJECT }, new int[] {
R.id.Subject,});
setListAdapter(adapter);
}
更新
protected JSONObject doInBackground(String... args) {
JSONObject json = new JSONObject();
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com";
JSONParsser jParser = new JSONParsser();
json = jParser.readJSONFeed(URL);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
int count = 0;
try {
count = json.getJSONObject("all").getInt("count");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(count>0) {
pDialog.dismiss();
Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show();
finish();
}else{
try {
JSONArray questions = json.getJSONObject("all").getJSONArray("questions");
for(int i = 0; i < questions.length(); i++) {
JSONObject question = questions.getJSONObject(i);
String Subject = question.getString(TAG_QUESTION_SUBJECT);
String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = question.getString(TAG_QUESTION_CONTENT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_QUESTION_SUBJECT, Subject);
map.put(TAG_QUESTION_CONTENT, Content);
map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);
questionList.add(map);
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
R.layout.listelements,
new String[] { TAG_QUESTION_SUBJECT }, new int[] {
R.id.Subject,});
setListAdapter(adapter);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}