我已经开始研究android开发了,所以我以JSON格式从数据库请求数据,我得到的返回值就像“{”users“:[{”child_name“:”John“}]}”和这里是我用于显示响应的代码:
public class welcome扩展了Activity {
private static final String URL = "http://www.schools.weavearound.com/android_api/user_fetch.php";
private StringRequest request;
private RequestQueue requestQueue;
private Button log_out;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity);
// email = (EditText) findViewById(R.id.email);
final String strJson="";
final TextView testTextView = (TextView) findViewById(R.id.display);
requestQueue = Volley.newRequestQueue(this);
final TextView nameView = (TextView) findViewById(R.id.result_txt);
nameView.setText(getIntent().getExtras().getString("username"));
// fetch the db here
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.e("tag", e.getMessage());
String data = "";
try {
JSONObject jsonObject = new JSONObject(response);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonObject.optJSONArray("users");
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String name = jsonObject1.optString("child_name").toString();
data += "Node" + i + " : name= " + name +" \n ";
}
testTextView.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
但活动中没有显示任何值。请帮助我。
答案 0 :(得分:0)
在创建request
之后添加requestQueue
,如下所示:
requestQueue.add(request);