【问题标题】:Making A listview with JSON Data使用 JSON 数据制作列表视图
【发布时间】:2014-05-13 19:12:47
【问题描述】:

我开始在 android 中进行开发,我想从网站获取 JSON 数据并制作一个包含所有 ID 的 ListView。 我查了一下,我从网上获取了 jason rawdata,但是当我尝试将其设置为 Json 对象时,我迷路了,并且在完成了如何将我的 Json 数组设置为主 UI 上的 ListView 之后

XMl

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <ScrollView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ScroolView">
    <TextView  //for testing rawData
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/DataText" />
    </ScrollView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ScroolView"
        android:headerDividersEnabled="true" >

        </ListView>

</RelativeLayout>





 package com.example.getjson;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    TextView textView;
    ListView lv;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.DataText);         
        lv = (ListView) findViewById(R.id.listView1);

        Toast.makeText(getBaseContext(),"Start",Toast.LENGTH_LONG).show();
        DownloadData DLD= new DownloadData();
        DLD.execute();


    }

class DownloadData extends AsyncTask<String , Void , String>{
    String Data;
    String [] item;
@Override
    protected String doInBackground(String... Void) {
            RequestHtml rh= new RequestHtml();
            Data = rh.fatchData();
            item = rh.ftachArray();
            ArrayAdapter<String> adapt = 
                      new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item); 


        return Data;
    }

    @Override
    protected void onPostExecute (String text){

        textView.setText(Data);



    }



}


}

以及我如何从网站获取数据

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class RequestHtml {

        JSONArray contacts = null;
    String Url="http://api.androidhive.info/contacts/";

    //public ArrayList<Emploee> FatchEmploee(){
    public String fatchData(){
        //ArrayList<Emploee> allEmploeeys = new ArrayList<Emploee>();
        Networkconnection network = new Networkconnection();
        String RawResponse = new String();




            try {
                RawResponse = network.request(Url);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



                return RawResponse;

    }

    public String [] ftachArray(){
        String[] JS= new String []{};
        Networkconnection network = new Networkconnection();
        String RawResponse1 = new String();




            try {
                RawResponse1 = network.request(Url);

                JSONObject jo = new JSONObject (RawResponse1);
                JSONArray contactsArray = jo.getJSONArray("contacts");
                for (int i =0; i<contactsArray.length(); i++){

                    JSONObject jasonid = contactsArray.getJSONObject(i);
                    String id = jasonid.getString("id");
                    JS [i] = id; 


                }


            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        return JS;
    }

}

public class Networkconnection {

    public String request(String Url) throws ClientProtocolException, IOException{

        // make request
        HttpGet httpGet = new HttpGet(Url);
        Log.d("Http", "httpGet");
        // make handler to handler the data
        ResponseHandler<String> responsehandler = new BasicResponseHandler();
        Log.d("Http", "Hendler");
        //Http client glues together the above
        HttpClient httpClient = new DefaultHttpClient();
        Log.d("Http", "httpclient");
        //make the connection
        String response = httpClient.execute(httpGet,responsehandler);
            Log.d("Http", "response" + response );


        return response;
    }



}

更新: Json 看起来像 http://api.androidhive.info/contacts/ Json 被正确解析 Json 是对的问题是如何将数据从 fatchArray() 导出到 MainActivity?

感谢大家的帮助

【问题讨论】:

标签: android json listview


【解决方案1】:

确保您首先收到 JSON 响应。看起来实际响应是在 JSONObject 中返回一个 JSONArray。首先创建一个 JSONObject,然后对其执行 getJSONArray()。

JSONObject responseJson = new JSONObject(RawResponse);
JSONArray contactsJsonArray = responseJson.getJSONArray("contacts");

然后遍历contactsJsonArray得到实际的联系人JSONObject并解析。

【讨论】:

  • 等等。你为什么不完全关注这篇文章(androidhive.info/2012/01/android-json-parsing-tutorial)?特别是做 HTTP Get 部分。看起来您仍然在使用本教程来获取联系人列表。
  • 我进一步使用了 Jason 示例的指南,该指南使用 mainactivity 扩展和 ListActivity,我正在使用 extnds Activity
【解决方案2】:

创建一个扩展 BaseAdapter 的类,并在其构造函数中将 JSONArray 传递给它。然后覆盖适配器的 getItem 以从数组中获取正确的元素,覆盖 getView 以膨胀一些视图 - simple_list_item1 或类似的。然后只需将适配器设置为列表视图的适配器。

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多