我正在开发一个应用程序,我需要从互联网上获取一些数据,如名称和电子邮件,我看了一些关于如何使用volley来解析JSON文件并从互联网上获取数据的教程我想在ListView中显示一些数据(JSON文件中的所有名称),但是当我运行它时我的应用程序崩溃了,这是一个简化的代码:
public class MainActivity extends Activity {
// json array response url
private String urlJsonArry = "https://api.myjson.com/bins/1d60wu";
private String jsonResponse;
ListView nameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nameList = (ListView) findViewById(R.id.nameList);
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
ArrayList al = new ArrayList();
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
// al.add(response.get(i));
al.add(person.getString("name"));
}
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, al);
nameList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(req);
}}
JSON CODE
[
{
"name": "Ravi Tamada",
"email": "ravi8x@gmail.com",
"phone": {
"home": "08947 000000",
"mobile": "9999999999"
}
},
{
"name": "opeyemi sanusi",
"email": "rffddg@gmail.com",
"phone": {
"home": "08457 000000",
"mobile": "999944999"
}
},
{
"name": "james bond",
"email": "dfgdg@gmail.com",
"phone": {
"home": "05657 000000",
"mobile": "999944999"
}
},
{
"name": "Tommy",
"email": "tommy@gmail.com",
"phone": {
"home": "08946 000000",
"mobile": "0000000000"
}
}
]
我只想获得listview中显示的每个数组中的名称,即Ravi Tamada,opeyemi Sanusi,James bond等
答案 0 :(得分:1)
您可以更换下面的请求代码,但不会遇到崩溃。我还检查了我的设备,现在我可以用数组填充listview。 并且你不要忘记在清单中允许互联网。
StringRequest req = new StringRequest(urlJsonArry,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
ArrayList al = new ArrayList();
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.has("name")) {
al.add(jsonObject.getString("name"));
}
}
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, al);
nameList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(req);
答案 1 :(得分:0)
setContentView(R.layout.activity_main);
失踪了。
此代码可以正常工作
package com.radline.radapp.radapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends Activity {
String urlJsonArry = "https://api.myjson.com/bins/1d60wu";
ListView nameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameList = (ListView) findViewById(R.id.nameList);
StringRequest req = new StringRequest(urlJsonArry,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
ArrayList al = new ArrayList();
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.has("name")) {
al.add(jsonObject.getString("name"));
}
}
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, al);
nameList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error", "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(req);
}
}