Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1077780
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-25 19:35:34

Xml代码 复制代码 收藏代码
  1. xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" android:text="@string/hello" />
  7. <Button android:text="直接获取数据" android:id="@+id/Button01"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content">
  10. Button>
  11. <Button android:text="GET方式传递" android:id="@+id/Button02"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content">
  14. Button>
  15. <Button android:text="POST方式传递" android:id="@+id/Button03"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content">
  18. Button>
  19. <Button android:text="获取图片" android:id="@+id/Button04"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content">
  22. Button>
  23. LinearLayout>


Java代码 复制代码 收藏代码
  1. package com.Aina.Android;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. public class Test extends Activity {
  8. /** Called when the activity is first created. */
  9. private Button btn1 = null;
  10. private Button btn2 = null;
  11. private Button btn3 = null;
  12. private Button btn4 = null;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. btn1 = (Button) this.findViewById(R.id.Button01);
  18. btn2 = (Button) this.findViewById(R.id.Button02);
  19. btn3 = (Button) this.findViewById(R.id.Button03);
  20. btn4 = (Button) this.findViewById(R.id.Button04);
  21. btn1.setOnClickListener(new Button.OnClickListener(){
  22. public void onClick(View v) {
  23. // TODO Auto-generated method stub
  24. Intent intent = new Intent();
  25. intent.setClass(Test.this, ShowData.class);
  26. Bundle b = new Bundle();
  27. b.putInt("id", 1);
  28. intent.putExtras(b);
  29. startActivity(intent);
  30. }
  31. });
  32. btn2.setOnClickListener(new Button.OnClickListener(){
  33. public void onClick(View v) {
  34. // TODO Auto-generated method stub
  35. Intent intent = new Intent();
  36. intent.setClass(Test.this, ShowData.class);
  37. Bundle b = new Bundle();
  38. b.putInt("id", 2);
  39. intent.putExtras(b);
  40. startActivity(intent);
  41. }
  42. });
  43. btn3.setOnClickListener(new Button.OnClickListener(){
  44. public void onClick(View v) {
  45. // TODO Auto-generated method stub
  46. Intent intent = new Intent();
  47. intent.setClass(Test.this, ShowData.class);
  48. Bundle b = new Bundle();
  49. b.putInt("id", 3);
  50. intent.putExtras(b);
  51. startActivity(intent);
  52. }
  53. });
  54. btn4.setOnClickListener(new Button.OnClickListener(){
  55. public void onClick(View v) {
  56. // TODO Auto-generated method stub
  57. Intent intent = new Intent();
  58. intent.setClass(Test.this, ShowData.class);
  59. Bundle b = new Bundle();
  60. b.putInt("id", 4);
  61. intent.putExtras(b);
  62. startActivity(intent);
  63. }
  64. });
  65. }
  66. }
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代码 复制代码 收藏代码
  1. xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView android:id="@+id/TextView_HTTP"
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content" />
  8. <ImageView android:id="@+id/ImageView01"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content">
  11. ImageView>
  12. LinearLayout>


Java代码 复制代码 收藏代码
  1. package com.Aina.Android;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.net.URLEncoder;
  12. import android.app.Activity;
  13. import android.content.Intent;
  14. import android.graphics.Bitmap;
  15. import android.graphics.BitmapFactory;
  16. import android.os.Bundle;
  17. import android.util.Log;
  18. import android.widget.ImageView;
  19. import android.widget.TextView;
  20. /**
  21. * com.Aina.Android Pro_HttpURLConnection
  22. *
  23. * @author Aina.huang E-mail: 674023920@qq.com
  24. * @version 创建时间:2010 Jul 12, 2010 2:08:40 PM 类说明
  25. */
  26. public class ShowData extends Activity {
  27. private TextView tv = null;
  28. private ImageView iv = null;
  29. private Bitmap mBitmap = null;
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.http);
  34. Intent intent = this.getIntent();
  35. Bundle b = intent.getExtras();
  36. int id = b.getInt("id");
  37. tv = (TextView) this.findViewById(R.id.TextView_HTTP);
  38. iv = (ImageView) this.findViewById(R.id.ImageView01);
  39. if (id == 1) {
  40. String httpUrl = "";
  41. URL url = null;
  42. try {
  43. url = new URL(httpUrl);
  44. } catch (MalformedURLException e) {
  45. e.printStackTrace();
  46. }
  47. if (url != null) {
  48. try {
  49. HttpURLConnection urlConn = (HttpURLConnection) url
  50. .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接
  51. urlConn.connect();// 连接
  52. InputStream input = urlConn.getInputStream();
  53. InputStreamReader inputReader = new InputStreamReader(input);
  54. BufferedReader reader = new BufferedReader(inputReader);
  55. String inputLine = null;
  56. StringBuffer sb = new StringBuffer();
  57. while ((inputLine = reader.readLine()) != null) {
  58. sb.append(inputLine).append("\n");
  59. }
  60. reader.close();
  61. inputReader.close();
  62. input.close();
  63. urlConn.disconnect();
  64. if(sb !=null){
  65. tv.setText(sb.toString());
  66. }else{
  67. tv.setText("读取的内容:NULL");
  68. }
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }else{
  73. Log.i("TAG", "url is null");
  74. }
  75. }else if(id==2){
  76. String httpUrl = "";
  77. URL url = null;
  78. try {
  79. url = new URL(httpUrl);
  80. } catch (MalformedURLException e) {
  81. e.printStackTrace();
  82. }
  83. if (url != null) {
  84. try {
  85. HttpURLConnection urlConn = (HttpURLConnection) url
  86. .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接
  87. urlConn.setDoInput(true);
  88. urlConn.setDoOutput(true);
  89. urlConn.connect();// 连接
  90. InputStream input = urlConn.getInputStream();
  91. InputStreamReader inputReader = new InputStreamReader(input);
  92. BufferedReader reader = new BufferedReader(inputReader);
  93. String inputLine = null;
  94. StringBuffer sb = new StringBuffer();
  95. while ((inputLine = reader.readLine()) != null) {
  96. sb.append(inputLine).append("\n");
  97. }
  98. reader.close();
  99. inputReader.close();
  100. input.close();
  101. urlConn.disconnect();
  102. if(sb !=null){
  103. tv.setText(sb.toString());
  104. }else{
  105. tv.setText("读取的内容:NULL");
  106. }
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }else{
  111. Log.i("TAG", "url is null");
  112. }
  113. }else if(id==3){
  114. String httpUrl = "";
  115. URL url = null;
  116. try {
  117. url = new URL(httpUrl);
  118. } catch (MalformedURLException e) {
  119. e.printStackTrace();
  120. }
  121. if (url != null) {
  122. try {
  123. HttpURLConnection urlConn = (HttpURLConnection) url
  124. .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接
  125. urlConn.setDoInput(true);
  126. urlConn.setDoOutput(true);
  127. urlConn.setRequestMethod("POST");
  128. urlConn.setUseCaches(false);//post请求不能使用缓存.
  129. urlConn.setInstanceFollowRedirects(true);//是否自动重定向.
  130. urlConn.connect();// 连接
  131. OutputStream out = urlConn.getOutputStream();
  132. DataOutputStream data = new DataOutputStream(out);
  133. data.writeBytes("par="+URLEncoder.encode("hk", "GBK"));
  134. data.flush();
  135. data.close();
  136. out.close();
  137. InputStream input = urlConn.getInputStream();
  138. InputStreamReader inputReader = new InputStreamReader(input);
  139. BufferedReader reader = new BufferedReader(inputReader);
  140. String inputLine = null;
  141. StringBuffer sb = new StringBuffer();
  142. while ((inputLine = reader.readLine()) != null) {
  143. sb.append(inputLine).append("\n");
  144. }
  145. reader.close();
  146. inputReader.close();
  147. input.close();
  148. urlConn.disconnect();
  149. if(sb !=null){
  150. tv.setText(sb.toString());
  151. }else{
  152. tv.setText("读取的内容:NULL");
  153. }
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. }
  157. }else{
  158. Log.i("TAG", "url is null");
  159. }
  160. }else if(id==4){
  161. String httpUrl = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.gif";
  162. URL url = null;
  163. try {
  164. url = new URL(httpUrl);
  165. } catch (MalformedURLException e) {
  166. e.printStackTrace();
  167. }
  168. if (url != null) {
  169. try {
  170. HttpURLConnection urlConn = (HttpURLConnection) url
  171. .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接
  172. urlConn.connect();// 连接
  173. InputStream input = urlConn.getInputStream();
  174. mBitmap = BitmapFactory.decodeStream(input);
  175. if(mBitmap != null){
  176. iv.setImageBitmap(mBitmap);
  177. }
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. }else{
  182. Log.i("TAG", "url is null");
  183. }
  184. }
  185. }
  186. }
阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~