【发布时间】:2017-06-14 22:00:49
【问题描述】:
当我使用下面的代码在 Logcat 中打印 getCategory 时,我得到 the category is Plumber(或任何 review.setCategory 设置的值。)
//post the review that has been clicked in the ListView and send it to
// ContactView.php and from that get other review details
StringRequest stringRequest = new StringRequest(Request.Method.POST, ContactView_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hide the 'loading' box when the page loads
pDialog.dismiss();
//toast the response of ContactView.php, which has been converted to a
//JSON array in the Php file with JSON encode
Toast.makeText(ContactView.this, response, Toast.LENGTH_LONG).show();
for (int i = 0; i < response.length(); i++) {
getCategory = null;
try {
JSONArray responseObject = new JSONArray(response);
JSONObject obj = responseObject.getJSONObject(i);
Review review = new Review();
review.setCategory(obj.getString("category"));
review.setName(obj.getString("name"));
review.setPhone(obj.getString("phone"));
review.setAddress(obj.getString("address"));
review.setComment(obj.getString("comment"));
//we are getting the review id so we can pull extra needed info, like Address etc
//review.setReviewid(obj.getString("reviewid"));
Toast.makeText(ContactView.this, review.getCategory(), Toast.LENGTH_SHORT).show();
//reviewList.add(review);
getCategory = review.getCategory();
System.out.println("the category is " + getCategory);
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
}
但是当我将System.out.println("the category is " + getCategory); 放在活动中的任何其他位置时,try-catch 之外,我得到the category is null
我阅读了 Stackoverflow 上的许多帖子,通过在 try-catch 块之前声明 getCategory = null; 来解决这个问题,但 t 并没有解决它。任何帮助将不胜感激。
【问题讨论】: