首页 > 代码库 > android中网络请求数据,解析并添加到Listview中
android中网络请求数据,解析并添加到Listview中
今天我实现了网络请求数据,解析并将数据添加到Listview中。上个星期我有提到说要实现点击Listview中item实现不同的功能,还是没头绪。如有幸被阅读,希望指教一下。我用的是hTTPClient的post方法请求的数据。在实现请求之前,还有一个主activity跳转。代码如下:
请求activity代码:
public class MachineInterface extends Activity {
private ListView machineType;
private ArrayList<MachineParams> machineList = new ArrayList<MachineParams>();
private MachineAdapter adapter;
private MachineParams mParams;
private StringBuilder builder;
String line = null;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what == 0x123){
machineType.setAdapter(adapter);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.machine_interface);
machineType = (ListView) findViewById(R.id.list_machine_type);
Thread thread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
HttpParams params = new BasicHttpParams();//定义网络通信的接口
HttpConnectionParams.setConnectionTimeout(params, 5000);//连接超时为5秒
HttpConnectionParams.setSoTimeout(params, 5000);//数据传输超时为5秒
HttpClient client = new DefaultHttpClient(params);//客服端的实现
//post方法请求
HttpPost post = new HttpPost("http://www.bomeans.com/api_v2/getTypeList.php?lang=tw&api_key=80b546857c51b8dd53bae4fd9cec784605477ebbf");
InputStream inStream = null;
try {
HttpResponse response = client.execute(post);//响应post请求
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity = response.getEntity();//实体对象
inStream = entity.getContent();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
builder = new StringBuilder();
while((line = bReader.readLine()) != null){
builder.append(line);
Log.e("builder", builder.toString());
}
JSONObject jsonObject = new JSONObject(builder.toString());
Log.e("jsonObject", jsonObject.toString());
// JSONArray array = new JSONObject(builder.toString()).getJSONArray("data");//也可以这么用
JSONArray array = jsonObject.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array.opt(i);
mParams = new MachineParams();
mParams.setMdName(object.getString("md_name"));
machineList.add(mParams);
Log.e("@11", "#114");
adapter = new MachineAdapter(MachineInterface.this, machineList);
handler.sendEmptyMessage(0x123);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.setContextClassLoader(getClass().getClassLoader());
thread.start();、
自定义适配器代码:
public class MachineAdapter extends BaseAdapter {
private Context context;
private ArrayList<MachineParams> machineList = new ArrayList<MachineParams>();
public MachineAdapter(Context context,ArrayList<MachineParams> machineList){
this.context = context;
this.machineList = machineList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return machineList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = LayoutInflater.from(context).inflate(R.layout.adapter, null);
TextView machineName = (TextView) convertView.findViewById(R.id.text_machine);
machineName.setText(machineList.get(position).getMdName());
return convertView;
}
}
属性类就一个属性。
android中网络请求数据,解析并添加到Listview中