结果如图:
注意:服务器与客户端无法链接的可能原因有: 没有加访问网络的权限:<uses-permission android:name="android.permission.INTERNET"></uses-permission> IP地址要使用:10.0.2.2 模拟器不能配置代理。 2。Apache接口 对于大部分应用程序而言JDK本身提供的网络功能已远远不够,这时就需要Android提供的Apache HttpClient了。它是一个开源项目,功能更加完善,为客户端的Http编程提供高效、最新、功能丰富的工具包支持。 下面我们以一个简单例子来看看如何使用HttpClient在Android客户端访问Web。 首先,要在你的机器上搭建一个web应用myapp,只有很简单的一个http.jsp 内容如下: [java] view plain copy <%@page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title> Http Test </title> </head> <body> <% String type = request.getParameter("parameter"); String result = new String(type.getBytes("iso-8859-1"),"utf-8"); out.println("<h1>" + result + "</h1>"); %> </body> </html> 然后实现Android客户端,分别以post、get方式去访问myapp,代码如下: 布局文件: [java] view plain copy <?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:gravity="center" android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="get" android:id="@+id/get" android:layout_width="match_parent" android:layout_height="wrap_content"></Button> <Button android:text="post" android:id="@+id/post" android:layout_width="match_parent" android:layout_height="wrap_content"></Button> </LinearLayout> 资源文件: strings.xml [java] view plain copy <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">通过按钮选择不同方式访问网页</string> <string name="app_name">Http Get</string> </resources> 主Activity: [java] view plain copy public class MainActivity extends Activity { private TextView textView; private Button get,post; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView)findViewById(R.id.textView); get = (Button)findViewById(R.id.get); post = (Button)findViewById(R.id.post); //绑定按钮监听器 get.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //注意:此处ip不能用127.0.0.1或localhost,Android模拟器已将它自己作为了localhost String uri = "http://192.168.22.28:8080/myapp/http.jsp?parameter=以Get方式发送请求"; textView.setText(get(uri)); } }); //绑定按钮监听器 post.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String uri = "http://192.168.22.28:8080/myapp/http.jsp"; textView.setText(post(uri)); } }); } /** * 以get方式发送请求,访问web * @param uri web地址 * @return 响应数据 */ private static String get(String uri){ BufferedReader reader = null; StringBuffer sb = null; String result = ""; HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(uri); try { //发送请求,得到响应 HttpResponse response = client.execute(request); //请求成功 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); sb = new StringBuffer(); String line = ""; String NL = System.getProperty("line.separator"); while((line = reader.readLine()) != null){ sb.append(line); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if (null != reader){ reader.close(); reader = null; } } catch (IOException e) { e.printStackTrace(); } } if (null != sb){ result = sb.toString(); } return result; } /** * 以post方式发送请求,访问web * @param uri web地址 * @return 响应数据 */ private static String post(String uri){ BufferedReader reader = null; StringBuffer sb = null; String result = ""; HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(uri); //保存要传递的参数 List<NameValuePair> params = new ArrayList<NameValuePair>(); //添加参数 params.add(new BasicNameValuePair("parameter","以Post方式发送请求")); try { //设置字符集 HttpEntity entity = new UrlEncodedFormEntity(params,"utf-8"); //请求对象 request.setEntity(entity); //发送请求 HttpResponse response = client.execute(request); //请求成功 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ System.out.println("post success"); reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); sb = new StringBuffer(); String line = ""; String NL = System.getProperty("line.separator"); while((line = reader.readLine()) != null){ sb.append(line); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { //关闭流 if (null != reader){ reader.close(); reader = null; } } catch (IOException e) { e.printStackTrace(); } } if (null != sb){ result = sb.toString(); } return result; } }
运行结果如下:
3.android.net编程: 常常使用此包下的类进行Android特有的网络编程,如:访问WiFi,访问Android联网信息,邮件等功能。这里不详细讲。
