简单Android HttpURLConnection---Get方式
Android 使用Http 链接获取数据的方式有两种 一个是HttpURLConnection 另一个是HttpClient。
每一种都有两种传输方式:GET 和 POST;(另外还有专门的HttpGet 和HttpPost 方式)
其二者区别:
- GET请求请提交的数据放置在HTTP请求协议头中(http://www.xxx.com/xxx?action="xxx"&action2="xxx");而POST提交的数据则放在实体数据中(使用NameValuePair来保存要传递的Post参数);
- GET方式提交的数据最多只能有1024字节,而POST则没有此限制;
这里主要介绍 HttpConnection 的子类 HttpURLConnection;
核心代码:
View Code
1 URL url = new URL(urlString); 2 HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); 3 //设置超时时间 4 urlConn.setConnectTimeout(1000); 5 //链接状态 6 int code = urlConn.getResponseCode(); 7 if(code == HttpURLConnection.HTTP_OK){ 8 //得到读取的内容(流) 9 InputStreamReader in = new InputStreamReader(urlConn.getInputStream()); 10 // 为输出创建BufferedReader 11 BufferedReader buffer = new BufferedReader(in); 12 String inputLine = null; 13 //使用循环来读取获得的数据 14 while (((inputLine = buffer.readLine()) != null)) 15 { 16 //我们在每一行后面加上一个"\n"来换行 17 resultData += inputLine + "\n"; 18 } 19 //关闭InputStreamReader 20 in.close(); 21 } 22 //关闭http连接 23 urlConn.disconnect();
这个是HttpURLConnection 的Get的方式,无需设置,默认为Get方式,如要Post方式则 setDoOutput(ture) ,其他方式(OPTIONS
, HEAD
, PUT
, DELETE
and TRACE
) 则使用setRequestMethod(String).
全部代码:
View Code
1 public class HttpConnActivity extends Activity { 2 3 String googleWeatherUrl2 = "http://m.weather.com.cn/data/101010100.html"; 4 private String resultData = null; 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_json); 9 Thread mThread = new Thread(GetWeatherDataThread); 10 mThread.start(); 11 } 12 13 private Runnable GetWeatherDataThread = new Runnable() { 14 15 @Override 16 public void run() { 17 // TODO Auto-generated method stub 18 try { 19 getWeatherData(); 20 } catch (IOException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 mHandler.sendEmptyMessage(0); 25 } 26 }; 27 28 Handler mHandler = new Handler(){ 29 30 @Override 31 public void handleMessage(Message msg) { 32 // TODO Auto-generated method stub 33 super.handleMessage(msg); 34 if(msg.what == 0){ 35 System.out.println(resultData); 36 System.out.println("线程结束"); 37 } 38 } 39 40 }; 41 42 public void getWeatherData() throws IOException { 43 URL url = new URL(googleWeatherUrl2); 44 HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); 45 urlConn.setConnectTimeout(1000); 46 int code = urlConn.getResponseCode(); 47 if(code == HttpURLConnection.HTTP_OK){ 48 //得到读取的内容(流) 49 InputStreamReader in = new InputStreamReader(urlConn.getInputStream()); 50 // 为输出创建BufferedReader 51 BufferedReader buffer = new BufferedReader(in); 52 String inputLine = null; 53 //使用循环来读取获得的数据 54 while (((inputLine = buffer.readLine()) != null)) 55 { 56 //我们在每一行后面加上一个"\n"来换行 57 resultData += inputLine + "\n"; 58 } 59 //关闭InputStreamReader 60 in.close(); 61 } 62 //关闭http连接 63 urlConn.disconnect(); 64 } 65 @Override 66 public boolean onCreateOptionsMenu(Menu menu) { 67 getMenuInflater().inflate(R.menu.activity_json, menu); 68 return true; 69 } 70 }