网络请求类:
package com.example.liangminghuitwo; import java.io.IOException; import org.apache.http.HttpClientConnection; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; public class Myutils { public static String getData(String url){ String result=null; HttpGet get=new HttpGet(url); //设置参数 HttpParams params=new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); //创建HTTPClinent HttpClient client=new DefaultHttpClient(params); try { HttpResponse httpResponse=client.execute(get); if(httpResponse.getStatusLine().getStatusCode()==200){ HttpEntity httpEntity=httpResponse.getEntity(); result=EntityUtils.toString(httpEntity, "utf-8"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } }
MainActivity.class:
package com.example.liangminghuitwo; import java.util.ArrayList; import java.util.List; import com.example.adapter.Mybase; import com.example.vo.Data; import com.example.vo.Demo; import com.google.gson.Gson; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView list_view; private TextView text_find; private EditText edit_find; String urls="http://93.gov.cn/93app/get_search.do?key="; private List<Demo> data=new ArrayList<Demo>(); private String st; Handler han=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); String rs=(String) msg.obj; Gson g=new Gson(); Data de=g.fromJson(rs, Data.class); data=de.getData(); Mybase base=new Mybase(data, getApplicationContext()); list_view.setAdapter(base); } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_view=(ListView) findViewById(R.id.list_view); text_find=(TextView) findViewById(R.id.text_find); edit_find=(EditText) findViewById(R.id.edit_find); st=text_find.getText().toString().trim(); text_find.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(){ public void run() { final String name=edit_find.getText().toString().trim(); String str=Myutils.getData(urls+name); han.sendMessage(han.obtainMessage(1,str)); }; }.start(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
以及添加适配器:Mybase.class
package com.example.adapter; import java.util.List; import com.example.liangminghuitwo.R; import com.example.vo.Demo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class Mybase extends BaseAdapter{ private List<Demo> data; private Context context; public Mybase(List<Demo> data, Context context) { super(); this.data = data; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return data.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.get(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 Viehoder vh; if(convertView==null){ vh=new Viehoder(); convertView=LayoutInflater.from(context).inflate(R.layout.item,null); vh.text_time=(TextView) convertView.findViewById(R.id.text_time); vh.text_two=(TextView) convertView.findViewById(R.id.text_two); vh.text_title=(TextView) convertView.findViewById(R.id.text_title); convertView.setTag(vh); }else{ vh=(Viehoder) convertView.getTag(); } vh.text_time.setText(data.get(position).getSHOWTIME()); vh.text_title.setText(data.get(position).getTITLE()); vh.text_two.setText(data.get(position).getFROMNAME()); return convertView; } class Viehoder{ TextView text_title,text_two,text_time; } }
效果图: