在那里我开始在Android中开发,我想从网站获取JSON数据并制作所有ID的ListView。 我乱了,我从网上得到了jason rawdata,但是当我尝试将它变成Json对象时,我得到了Lost并且在制作了如何将我的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是对的qustion是如何将数据从fatchArray()导出到MainActivity?
感谢帮助
答案 0 :(得分:0)
确保首先获得JSON响应。看起来实际响应在JSONObject中返回JSONArray。首先创建一个JSONObject,然后对此执行getJSONArray()。
JSONObject responseJson = new JSONObject(RawResponse);
JSONArray contactsJsonArray = responseJson.getJSONArray("contacts");
然后遍历contactsJsonArray以获取实际联系人JSONObject并解析。
答案 1 :(得分:0)
创建一个扩展BaseAdapter的类,并在其构造函数中传递JSONArray。然后重写Adapter的getItem以从数组中获取正确的元素,覆盖getView以扩展某些视图 - simple_list_item1或类似的。然后只需将adaptr设置为listview的适配器。