从url获取JSON数据,然后将其显示在android的ListView中
我从URL获取JSON数据,然后将其显示在MainActivity的ListView中。 我已经与JSON文件URL建立了正确的连接,并且能够将URL中的数据提取到字符串中,但是现在我尝试使用ListAdapter在ListView中显示它,但它没有显示其中的任何数据 我的JSON数据如下所示:从url获取JSON数据,然后将其显示在android的ListView中,android,json,listview,Android,Json,Listview,我从URL获取JSON数据,然后将其显示在MainActivity的ListView中。 我已经与JSON文件URL建立了正确的连接,并且能够将URL中的数据提取到字符串中,但是现在我尝试使用ListAdapter在ListView中显示它,但它没有显示其中的任何数据 我的JSON数据如下所示: { "result": 1, "data": [{ "id": 1, "name": "Aconitum", "url": "https:
{
"result": 1,
"data": [{
"id": 1,
"name": "Aconitum",
"url": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/7\/7e\/Aconitum_degenii.jpg"
},
{
"id": 2,
"name": "African lily",
"url": "http:\/\/i1.wp.com\/cottagegardenflowershop.co.uk\/wp-content\/uploads\/2014\/09\/1040668-90x90.jpg?ssl=1"
},
{
"id": 3,
"name": "Alpine thistle",
"url": "http:\/\/i1.wp.com\/cottagegardenflowershop.co.uk\/wp-content\/uploads\/2014\/09\/1040668-90x90.jpg?ssl=1"
},
我以hashmap的形式将数据存储在这种格式的arrayList中。
ArrayList>contactList
我的主要activity.java是:
package com.example.hsports.flowers;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
public static String url="http://development.easystartup.org/NO/Backend/flower.php";
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listview);
new GetContacts().execute();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url=contactList.get(position).get("url");
System.out.println("hello");
}
});
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("data");
// looping through All Contacts
for (int i = 0; i < 28; i++) {
JSONObject c = contacts.getJSONObject(i);
if(c==null)
{
continue;
}
String id = c.getString("id");
String name = c.getString("name");
String urlInjson = c.getString("url");
//String address = c.getString("address");
//String gender = c.getString("gender");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("url", urlInjson);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.support_simple_spinner_dropdown_item, new String[]{"name"}, new int[]{R.id.flowerName});
lv.setAdapter(adapter);
}
}
}
package com.example.hsport.flowers;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.Display;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类MainActivity扩展了AppCompatActivity{
公共静态字符串url=”http://development.easystartup.org/NO/Backend/flower.php";
私有字符串标记=MainActivity.class.getSimpleName();
私人对话;
私有ListView lv;
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList=新的ArrayList();
lv=(ListView)findViewById(R.id.ListView);
新建GetContacts().execute();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串url=contactList.get(position.get(“url”);
System.out.println(“你好”);
}
});
}
私有类GetContacts扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“数据”);
//通过所有触点循环
对于(int i=0;i<28;i++){
JSONObject c=contacts.getJSONObject(i);
如果(c==null)
{
继续;
}
字符串id=c.getString(“id”);
字符串名称=c.getString(“名称”);
字符串urlInjson=c.getString(“url”);
//字符串地址=c.getString(“地址”);
//字符串性别=c.getString(“性别”);
//单个联系人的tmp哈希映射
HashMap contact=新的HashMap();
//将每个子节点添加到HashMap key=>value
联系人:put(“id”,id);
联系人。填写(“姓名”,姓名);
contact.put(“url”,urlInjson);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
MainActivity.this,联系人列表,
R.layout.support_simple_spinner_dropdown_项,新字符串[]{“name”},新int[]{R.id.flowerName});
低压设置适配器(适配器);
}
}
}
如何从该列表视图中的Arraylist中填充“name”?此语句中应该有一个更改:
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.support_simple_spinner_dropdown_item, new String[]{"name"}, new int[]{R.id.flowerName});
<