【发布时间】: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?
感谢大家的帮助
【问题讨论】:
-
JSON 是什么样的?