Android HttpURLConnection网络通信
2011-08-09 15:23
465 查看
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="直接获取数据" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="GET方式传递" android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="POST方式传递" android:id="@+id/Button03" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="获取图片" android:id="@+id/Button04" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> </LinearLayout>
Test Activtity
package com.Aina.Android; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Test extends Activity { /** Called when the activity is first created. */ private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button) this.findViewById(R.id.Button01); btn2 = (Button) this.findViewById(R.id.Button02); btn3 = (Button) this.findViewById(R.id.Button03); btn4 = (Button) this.findViewById(R.id.Button04); btn1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(Test.this, ShowData.class); Bundle b = new Bundle(); b.putInt("id", 1); intent.putExtras(b); startActivity(intent); } }); btn2.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(Test.this, ShowData.class); Bundle b = new Bundle(); b.putInt("id", 2); intent.putExtras(b); startActivity(intent); } }); btn3.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(Test.this, ShowData.class); Bundle b = new Bundle(); b.putInt("id", 3); intent.putExtras(b); startActivity(intent); } }); btn4.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(Test.this, ShowData.class); Bundle b = new Bundle(); b.putInt("id", 4); intent.putExtras(b); startActivity(intent); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/TextView_HTTP" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> </LinearLayout>
package com.Aina.Android; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.TextView; /** * com.Aina.Android Pro_HttpURLConnection * * @author Aina.huang E-mail: 674023920@qq.com * @version 创建时间:2010 Jul 12, 2010 2:08:40 PM 类说明 */ public class ShowData extends Activity { private TextView tv = null; private ImageView iv = null; private Bitmap mBitmap = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.http); Intent intent = this.getIntent(); Bundle b = intent.getExtras(); int id = b.getInt("id"); tv = (TextView) this.findViewById(R.id.TextView_HTTP); iv = (ImageView) this.findViewById(R.id.ImageView01); if (id == 1) { String httpUrl = "http://192.168.0.132:8080/Android/http.jsp"; URL url = null; try { url = new URL(httpUrl); } catch (MalformedURLException e) { e.printStackTrace(); } if (url != null) { try { HttpURLConnection urlConn = (HttpURLConnection) url .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接 urlConn.connect();// 连接 InputStream input = urlConn.getInputStream(); InputStreamReader inputReader = new InputStreamReader(input); BufferedReader reader = new BufferedReader(inputReader); String inputLine = null; StringBuffer sb = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { sb.append(inputLine).append("\n"); } reader.close(); inputReader.close(); input.close(); urlConn.disconnect(); if(sb !=null){ tv.setText(sb.toString()); }else{ tv.setText("读取的内容:NULL"); } } catch (IOException e) { e.printStackTrace(); } }else{ Log.i("TAG", "url is null"); } }else if(id==2){ String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp?par=hk"; URL url = null; try { url = new URL(httpUrl); } catch (MalformedURLException e) { e.printStackTrace(); } if (url != null) { try { HttpURLConnection urlConn = (HttpURLConnection) url .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接 urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.connect();// 连接 InputStream input = urlConn.getInputStream(); InputStreamReader inputReader = new InputStreamReader(input); BufferedReader reader = new BufferedReader(inputReader); String inputLine = null; StringBuffer sb = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { sb.append(inputLine).append("\n"); } reader.close(); inputReader.close(); input.close(); urlConn.disconnect(); if(sb !=null){ tv.setText(sb.toString()); }else{ tv.setText("读取的内容:NULL"); } } catch (IOException e) { e.printStackTrace(); } }else{ Log.i("TAG", "url is null"); } }else if(id==3){ String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp"; URL url = null; try { url = new URL(httpUrl); } catch (MalformedURLException e) { e.printStackTrace(); } if (url != null) { try { HttpURLConnection urlConn = (HttpURLConnection) url .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接 urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setRequestMethod("POST"); urlConn.setUseCaches(false);//post请求不能使用缓存. urlConn.setInstanceFollowRedirects(true);//是否自动重定向. urlConn.connect();// 连接 OutputStream out = urlConn.getOutputStream(); DataOutputStream data = new DataOutputStream(out); data.writeBytes("par="+URLEncoder.encode("hk", "GBK")); data.flush(); data.close(); out.close(); InputStream input = urlConn.getInputStream(); InputStreamReader inputReader = new InputStreamReader(input); BufferedReader reader = new BufferedReader(inputReader); String inputLine = null; StringBuffer sb = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { sb.append(inputLine).append("\n"); } reader.close(); inputReader.close(); input.close(); urlConn.disconnect(); if(sb !=null){ tv.setText(sb.toString()); }else{ tv.setText("读取的内容:NULL"); } } catch (IOException e) { e.printStackTrace(); } }else{ Log.i("TAG", "url is null"); } }else if(id==4){ String httpUrl = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.gif"; URL url = null; try { url = new URL(httpUrl); } catch (MalformedURLException e) { e.printStackTrace(); } if (url != null) { try { HttpURLConnection urlConn = (HttpURLConnection) url .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接 urlConn.connect();// 连接 InputStream input = urlConn.getInputStream(); mBitmap = BitmapFactory.decodeStream(input); if(mBitmap != null){ iv.setImageBitmap(mBitmap); } } catch (IOException e) { e.printStackTrace(); } }else{ Log.i("TAG", "url is null"); } } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Aina.Android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Test" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ShowData"></activity> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
来自http://aina-hk55hk.iteye.com/blog/710951
用做学习参考。
相关文章推荐
- Android HTTP网络通信(一):使用HttpURLConnection、HttpClient
- android 网络通信之HttpURLConnection与HttpClient
- 详细讲解Android的网络通信(HttpUrlConnection和HttpClient)
- Android——网络通信(HttpUrlConnection)
- Android中网络通信方式的简单汇总(HttpURLConnection、HttpClient、Socket等)
- Android的HTTP方式网络通信---HttpURLConnection
- Android HttpURLConnection 网络通信实时更新
- 【Android学习】网络通信----以HttpURLConnection Post请求为例
- android 网络通信之HttpURLConnection
- Android开发之网络通信(HttpURLConnection,HttpClient,HttpGet,HttpPost)
- Android开发之网络请求通信专题(一):基于HttpURLConnection的请求通信
- 详细讲解Android的网络通信(HttpUrlConnection和HttpClient)
- Android 网络通信——HttpURLConnection
- Android 网络通信之 HttpURLConnection
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- android网络通信之HttpUrlConnection
- Android学习篇章44-HttpURLConnection网络通信
- 【代码】Android的网络通信详解HttpUrlConnection和HttpClient
- Android入门笔记 - 网络通信 - HttpURLConnection
- Android网络通信之HttpURLConnection