如何使用 volley 将 json 文件解析为 Listview
how to parse a json file into a Listview with volley
我正在开发一个应用程序,我需要从互联网上获取一些数据,比如姓名和电子邮件,我看了一些关于如何使用 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 代码
[
{
"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"
}
}
]
我只想获取列表视图中显示的每个数组中的名称,即 - Ravi Tamada、opeyemi Sanusi、James bond 等
您好,像下面这样替换您的请求代码,您将不会遇到崩溃。我还检查了我的设备,现在我可以用数组填充列表视图。
而且你不要忘记在清单中授予互联网许可。
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);
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);
}
}
我正在开发一个应用程序,我需要从互联网上获取一些数据,比如姓名和电子邮件,我看了一些关于如何使用 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 代码
[
{
"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"
}
}
]
我只想获取列表视图中显示的每个数组中的名称,即 - Ravi Tamada、opeyemi Sanusi、James bond 等
您好,像下面这样替换您的请求代码,您将不会遇到崩溃。我还检查了我的设备,现在我可以用数组填充列表视图。 而且你不要忘记在清单中授予互联网许可。
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);
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);
}
}