Java 我想在android活动中显示json对象的值
我已经开始从事android开发工作,所以我正在请求一个JSON格式的数据库数据,我得到的返回值像“{”users:[{”child_name:“John”}]}”,下面是我用来显示响应的代码: 公开课欢迎活动{Java 我想在android活动中显示json对象的值,java,android,json,database,Java,Android,Json,Database,我已经开始从事android开发工作,所以我正在请求一个JSON格式的数据库数据,我得到的返回值像“{”users:[{”child_name:“John”}]}”,下面是我用来显示响应的代码: 公开课欢迎活动{ private static final String URL = "http://www.schools.weavearound.com/android_api/user_fetch.php"; private StringRequest request; private Reque
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) {
}
});
}
private静态最终字符串URL=”http://www.schools.weavearound.com/android_api/user_fetch.php";
私人请求;
私有请求队列请求队列;
私人按钮注销;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_活动);
//email=(EditText)findviewbyd(R.id.email);
最终字符串strJson=“”;
最终文本视图testTextView=(文本视图)findViewById(R.id.display);
requestQueue=Volley.newRequestQueue(this);
最终文本视图名称视图=(文本视图)findViewById(R.id.result\u txt);
nameView.setText(getIntent().getExtras().getString(“用户名”));
//在这里取数据库
request=new-StringRequest(request.Method.POST,URL,new-Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
//Log.e(“tag”,e.getMessage());
字符串数据=”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
//获取包含JSONObject的JSONArray实例
JSONArray JSONArray=jsonObject.optJSONArray(“用户”);
//迭代jsonArray并打印JSONObject的信息
for(int i=0;i
}
但活动中没有显示任何值。请在这方面帮助我。在创建
请求后将其添加到请求队列
,如下所示:
requestQueue.add(请求)
在将请求创建到请求队列后,将其添加到请求队列中,如下所示:
requestQueue.add(请求)代码>你能详细说明我在哪里、哪一部分出错吗?你能详细说明我在哪里、哪一部分出错吗?我现在尝试使用它,但它仍然没有显示TextViewLog VolleyError的值以检查你请求的问题。我现在尝试使用它,但它仍然没有显示TextViewLog VolleyError的值来检查您的请求的问题。