HttpURLConnection
来源:互联网 发布:golang mgo 编辑:程序博客网 时间:2024/06/08 07:34
public class MainActivity extends Activity { private ListView lv; private String urlpath="http://172.17.29.120/localuser/ljy/data/data.json"; private List<MyDate> list; private Handler handler=new Handler(){ public void dispatchMessage(Message msg) { switch (msg.what) { case 0: String result=(String) msg.obj; Gson gson = new Gson(); User user = gson.fromJson(result, User.class); list=user.data; MyAdapter adapter = new MyAdapter(list,MainActivity.this); lv.setAdapter(adapter); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); getResult(); } public void getResult(){ new Thread(){ public void run() { try { URL url = new URL(urlpath); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setReadTimeout(5000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); int code = httpURLConnection.getResponseCode(); if (code==200) { InputStream stream = httpURLConnection.getInputStream(); int len=0; byte[] by=new byte[1024]; StringBuffer stringBuffer=new StringBuffer(); while((len=stream.read(by))!=-1){ stringBuffer.append(new String(by, 0, len,"gbk")); String result = stringBuffer.toString(); Message message = new Message(); message.what=0; message.obj=result; handler.sendMessage(message); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); }}
0 0
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- ps -ef命令详解
- Hibernate基本概念
- Exynos4412裸机开发 —— RTC 实时时钟单元
- error C2144: 语法错误:“int”的前面应有“;”
- SpringMVC与Struts2的对比
- HttpURLConnection
- Android计算进程数及剩余内存
- PHP单个图片上传
- android 同心圆水波纹的实现
- 【CSS】CSS简介
- Exynos4412裸机开发——中断处理
- android 网络通信之HttpURLConnection与HttpClient
- Unity3D-Unity中CacheServer(缓存服务器)的部署与使用
- Largest BST Subtree