其他分享
首页 > 其他分享> > 信息的展示

信息的展示

作者:互联网

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class show00 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show00);
ListView listView = (ListView) this.findViewById(R.id.listView);
Handler mHandler = new Handler(){
@SuppressLint("HandlerLeak")
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:{
String x=(String) msg.obj;
Log.d("jsonstr", x);
JSONArray JsonObj=null;
try {
JsonObj= new JSONArray(x);
Log.d("JsonStrllll", JsonObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
int length=JsonObj.length();
ArrayList<newjb> list = new ArrayList<newjb>();
for(int i=0;i<length;i++){
newjb shi=null;
try {
JSONObject Json=JsonObj.getJSONObject(i);
shi=new newjb();
shi.setLeixing(Json.getString("leixing"));
shi.setChexing(Json.getString("chexing"));
shi.setChepai(Json.getString("chepai"));
shi.setYanse(Json.getString("yanse"));
shi.setTupian(Json.getString("tupian"));
list.add(shi);
} catch (JSONException e) {
e.printStackTrace();
}
}
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(newjb sg : list){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("leixing", sg.getLeixing());
item.put("chexing", sg.getChexing());
item.put("chepai", sg.getChepai());
item.put("yanse", sg.getYanse());
item.put("tupian", sg.getTupian());
data.add(item);
}
//创建SimpleAdapter适配器将数据绑定到item显示控件上
SimpleAdapter adapter = new SimpleAdapter(show00.this, data, R.layout.newitem,
new String[]{"leixing", "chexing", "chepai","yanse"}, new int[]{R.id.leixing, R.id.chexing, R.id.chepai01,R.id.yanse});
//实现列表的显示
listView.setAdapter(adapter);
//条目点击事件
listView.setOnItemClickListener(new ItemClickListener());
}
break;
}
}
};
new Thread(new Runnable() {
@Override
public void run() {//java的多线程这个必须有的
String x=http();
Message message = new Message();
message.what = 1;
message.obj = x;
mHandler.sendMessage(message);

}
}).start();

}
//获取点击事件
private final class ItemClickListener implements AdapterView.OnItemClickListener {

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listView = (ListView) parent;
HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);
newjb shi0 = new newjb();
shi0.setLeixing(data.get("leixing").toString());
shi0.setChexing(data.get("chexing").toString());
shi0.setChepai(data.get("chepai").toString());
shi0.setYanse(data.get("yanse").toString());
shi0.setTupian(data.get("tupian").toString());
Intent intent = new Intent(show00.this,
show000.class);
intent.putExtra("shi", (Serializable) shi0);
startActivity(intent);
}
}
public String http(){
HttpURLConnection huc=null;
URL url=null;
String x=null;
try {
url=new URL("http://43.138.34.77:8080/androidtest/servlet/zhanshi0");
huc=(HttpURLConnection)url.openConnection();
huc.setConnectTimeout(60000);
huc.setUseCaches(false);
huc.setInstanceFollowRedirects(true);
huc.setReadTimeout(60000);

huc.setDoInput(true);
huc.setDoOutput(true);
huc.setRequestMethod("POST");
huc.setRequestProperty("Content-Type","application/json;charset=UTF-8");
huc.connect();


String j="0";//把json赋值给字符串


OutputStream ups=huc.getOutputStream();//输出流
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(ups));//输出流转字符缓冲输出流
bw.write(j);//输出字符串j
bw.flush();
ups.close();//关闭输出流
bw.close();//关闭字符缓冲流
InputStream in = huc.getInputStream();
//读取输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
x=result.toString();
huc.getResponseCode();//这个必须有,得到Http的应答器
x=result.toString();
}catch (Exception e){
e.printStackTrace();
}finally {
huc.disconnect();
}

return x;
}
}

标签:shi,java,huc,展示,信息,new,import,android
来源: https://www.cnblogs.com/jidezan/p/16375723.html