networkmutilpledemoforhttpurlconnection 一个综合案例
2017-07-09 21:06
85 查看
package com.example.networkmutilpledemoforhttpurlconnection;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.networkmutilpledemoforhttpurlconnection.bean.MenuInfo;
import com.example.networkmutilpledemoforhttpurlconnection.utils.StreamTools;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.BitSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private List<MenuInfo.ResultBean> results;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
MenuInfo menuInfo = (MenuInfo) msg.obj;
results = menuInfo.getResult();
//设置适配器
lv.setAdapter(new MyBaseAdapter());
break;
case 2:
Toast.makeText(MainActivity.this, "请求失败!", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件 局部变量变全局变量快捷键 : Ctrl + Alt + F 之后敲Enter
lv = (ListView) findViewById(R.id.lv);
//获取网络数据
getData();
}
/**
* 获取网络数据
* GET
*/
private void getData() {
new Thread(){
@Override
public void run() {
super.run();
try {
URL url = new URL("http://apis.juhe.cn/catering/query?lng=121.538123&lat=31.677132&key=b1cc75a5744d69064bac2549993920f6");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5 * 1000);
connection.setRequestMethod("GET");
connection.setReadTimeout(5 * 1000);
//服务器响应
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
//得到服务器响应的内容数据
InputStream is = connection.getInputStream();
//数据有了
String json = StreamTools.readFromNetWork(is);
//Gson 基于JavaBean
Gson gson = new Gson();
MenuInfo menuInfo = gson.fromJson(json, MenuInfo.class);
//发送消息
Message msg = handler.obtainMessage();
msg.what = 1;
msg.obj = menuInfo;
handler.sendMessage(msg);
}else{
//请求数据失败 2
handler.sendEmptyMessage(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//自定义适配器
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return results != null ? results.size() : 0;
}
@Override
public Object getItem(int position) {
return results.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = convertView.inflate(MainActivity.this, android.R.layout.simple_list_item_1, null);
TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
//设置文本
text1.setText("饭店名称 : " + results.get(position).getName() + "\n饭店地址 : " +
"" + results.get(position).getAddress());
return convertView;
}
}
}
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.networkmutilpledemoforhttpurlconnection.bean.MenuInfo;
import com.example.networkmutilpledemoforhttpurlconnection.utils.StreamTools;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.BitSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private List<MenuInfo.ResultBean> results;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
MenuInfo menuInfo = (MenuInfo) msg.obj;
results = menuInfo.getResult();
//设置适配器
lv.setAdapter(new MyBaseAdapter());
break;
case 2:
Toast.makeText(MainActivity.this, "请求失败!", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件 局部变量变全局变量快捷键 : Ctrl + Alt + F 之后敲Enter
lv = (ListView) findViewById(R.id.lv);
//获取网络数据
getData();
}
/**
* 获取网络数据
* GET
*/
private void getData() {
new Thread(){
@Override
public void run() {
super.run();
try {
URL url = new URL("http://apis.juhe.cn/catering/query?lng=121.538123&lat=31.677132&key=b1cc75a5744d69064bac2549993920f6");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5 * 1000);
connection.setRequestMethod("GET");
connection.setReadTimeout(5 * 1000);
//服务器响应
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
//得到服务器响应的内容数据
InputStream is = connection.getInputStream();
//数据有了
String json = StreamTools.readFromNetWork(is);
//Gson 基于JavaBean
Gson gson = new Gson();
MenuInfo menuInfo = gson.fromJson(json, MenuInfo.class);
//发送消息
Message msg = handler.obtainMessage();
msg.what = 1;
msg.obj = menuInfo;
handler.sendMessage(msg);
}else{
//请求数据失败 2
handler.sendEmptyMessage(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
//自定义适配器
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return results != null ? results.size() : 0;
}
@Override
public Object getItem(int position) {
return results.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = convertView.inflate(MainActivity.this, android.R.layout.simple_list_item_1, null);
TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
//设置文本
text1.setText("饭店名称 : " + results.get(position).getName() + "\n饭店地址 : " +
"" + results.get(position).getAddress());
return convertView;
}
}
}
相关文章推荐
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
- 关于浮动和边距的一个综合案例
- 综合案例:输入一个整数判定是否素数
- Java线程——一个简单综合案例
- 一个综合案例来讲解servlet
- 一个使用上传图片插件,图文编辑,时间插件的综合案例
- 一个令人难忘的条形码案例
- SOA在一个典型企业应用案例中的应用构想
- 动态语言开发效能的一个案例研究
- 又一个本土化失败案例 ---中国让eBay了解本地化的重要性
- 配置三层交换的综合案例
- 不同操作系统之间迁移DB2数据库的一个案例
- 管理案例:一个日本小老板的工作记录
- 开放思路,综合考虑,心胸开阔,做一个合格的项目经理
- 一个很好的oracle案例分析blog
- 真实案例:给所有想要创业的朋友的一个管理方面的测试题
- 一个图书馆的借阅书刊的案例
- 如何成为一个优秀的综合布线培训讲师
- 网络资源--C#的一个简易的ftp服务器端(zz自C#网络应用案例导航)
- 一个表达式计算案例的设计和实现