//请求的网络的工具类
public class NetWorkUtils {
//请求网络数据public static String getString(String path) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置连接超时时间和读取超时时间
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5000);
//请求方式
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
//判断是否请求成功
if (code == 200) {
//将读取的数据转成字符串
InputStream inStream = conn.getInputStream();
int len = -1;
byte[] arr = new byte[1024];
StringBuilder builder = new StringBuilder();
while ((len = inStream.read(arr)) != -1) {
builder.append(new String(arr, 0, len));
}
return builder.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Maintivity
private ListView lv;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// Toast.makeText(MainActivity.this,"======="+string,Toast.LENGTH_SHORT).show();
//解析json
MyDataBean myDataBean = gson.fromJson(string, MyDataBean.class);
//获取集合
list = myDataBean.data;
//创建并设置适配器
MyAdapter adapter = new MyAdapter(MainActivity.this, list);
lv.setAdapter(adapter);
}
};
//请求地址
private String path="http://93.gov.cn/93app/get_search.do?&startNum=10&key=";
//定义控件和变量
private EditText et_ss;
private TextView tv_ss;
private String string;
private Gson gson;
private List<MyDataBean.Mydata> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//找控件
initView();
//设置点击事件
setlisener();
}
private void setlisener() {
//设置listview的条目点击事件
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,SuccessActivity.class);
intent.putExtra("id",list.get(position).ID);
startActivity(intent);
}
});
//设置搜索按钮的点击事件
tv_ss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = et_ss.getText().toString();
//拼接请求地址
path=path+ Uri.encode(str,"utf-8");
//子线程请求网络
new Thread(){
@Override
public void run() {
super.run();
//请求网络并返回字符串
string = NetWorkUtils.getString(path);
//handler发送消息跟新UI
handler.sendEmptyMessage(0);
}
}.start();
}
});
}
/**
* 找控件
*/
private void initView() {
//创建Gson
gson = new Gson();
//找控件
lv = (ListView) findViewById(R.id.lv);
et_ss = (EditText) findViewById(R.id.et_ss);
tv_ss = (TextView) findViewById(R.id.tv_ss);
}
}
InfoActivity
//获取Intent
Intent intent = getIntent();
//从Intent中取值
String id = intent.getStringExtra("id");
// Toast.makeText(this,"id=========="+id,Toast.LENGTH_SHORT).show();
//找控件
WebView web = (WebView) findViewById(R.id.web);
//拼接地址
String path = "http://www.93.gov.cn/93app/info.do?id="+id;
//给webview设置地址
web.loadUrl(path);
adapter
//定义变量
Context context;
List<MyDataBean.Mydata> list;
//构造方法传值
public MyAdapter(Context context, List<MyDataBean.Mydata> list) {
this.context=context;
this.list=list;
}
//返回条目个数
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//优化listview并展示图文混排的效果
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=View.inflate(context, R.layout.v_item,null );
holder=new ViewHolder(convertView);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
//给控件赋值
holder.getLvData().setText(list.get(position).SHOWTIME);
holder.getLvFromname().setText(list.get(position).FROMNAME);
holder.getLvTitle().setText(list.get(position).TITLE);
if(list.get(position).SUBTITLE!=null){
holder.getLvDes().setVisibility(View.VISIBLE);
holder.getLvDes().setText(list.get(position).SUBTITLE);
}
if(list.get(position).IMAGEURL!=null){
holder.getLvImg().setVisibility(View.VISIBLE);
ImageLoader.getInstance().displayImage(list.get(position).IMAGEURL,holder.getLvImg(), MyApplication.getDisplayImageOptions());
}
return convertView;
}
v_item.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:visibility="gone"
android:id="@+id/lv_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="@+id/lv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center_vertical"
android:textSize="20sp"
/>
<TextView
android:visibility="invisible"
android:id="@+id/lv_des"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center_vertical"
android:textSize="20sp"
/>
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="bottom"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv_fromname"
/>
<TextView
android:id="@+id/lv_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
infoActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="test.bwie.com.gaoxuge20160627.SuccessActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="搜索详情"
android:textSize="24sp" />
</LinearLayout>
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="test.bwie.com.gaoxuge20160627.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="24sp"
android:text="搜索" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_ss"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="20sp"
android:hint="请输入搜索内容" />
<TextView
android:id="@+id/tv_ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="搜索"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></ListView>
</LinearLayout>