这是一个简单实现了Android客户端与服务端交互-客户端GET方式登录和客户端POST方式查询的一个小案例

package com.jxust.day06_03_httpurlconnectiondemo;import java.io.IOException;public class MainActivity extends Activity {// 不能写localhost而应该写模拟机的地址10.0.2.2,如果是真机,那么就要写真机的地址// 访问的网络地址private static final String PATH = "http://10.0.2.2:8080/Day06_Servlet/Login_Servlet";EditText metName, metPassword;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setListener();}private void setListener() {setLoginOnClickListener();setQueryOnClickListener();}private void setQueryOnClickListener() {findViewById(R.id.btnQuery).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread() {public void run() {String name = metName.getText().toString();name = "name="+name;try {byte[] data = name.getBytes("utf-8");URL url = new URL(PATH);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setReadTimeout(5000);conn.setDoOutput(true); // doGet中是不需要设置的,但是doPost是需要设置的OutputStream out = conn.getOutputStream();out.write(data);out.flush();if (conn.getResponseCode() != 200) {return;}ObjectMapper om = new ObjectMapper();List<User> users = om.readValue(conn.getInputStream(), List.class);Log.i("main", users.toString());} catch (UnsupportedEncodingException e1) {e1.printStackTrace();} // 将类型转换成utf-8的格式catch (IOException e2) {e2.printStackTrace();}}}.start();}});}private void setLoginOnClickListener() {findViewById(R.id.btnLogin).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 创建一个工作线程,因为如果不在工作线程内访问网络有可能会出现异常new Thread() {public void run() {String name = metName.getText().toString();String password = metPassword.getText().toString();StringBuilder sb = new StringBuilder(PATH);sb.append("?name=").append(name).append("&password=").append(password);try {URL url = new URL(sb.toString());HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setReadTimeout(5000); // 读取服务端的时间为5秒钟,如果超过时间就断开服务端连接conn.setConnectTimeout(5000);conn.setRequestMethod("GET");if (conn.getResponseCode() != 200) {// Toast.makeText(MainActivity.this,// "连接服务端失败",// 2000).show();return;}ObjectMapper om = new ObjectMapper();User user = om.readValue(conn.getInputStream(), User.class); // 按User这个类型来解析Log.i("main", user.toString());} catch (MalformedURLException e1) {e1.printStackTrace();} catch (IOException e2) {e2.printStackTrace();}}}.start();}});}private void initView() {metName = (EditText) findViewById(R.id.etName);metPassword = (EditText) findViewById(R.id.etPassword);}}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:orientation="vertical"><EditText     android:id="@+id/etName"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="输入姓名"    android:text="菲"/><EditText     android:id="@+id/etPassword"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="输入登陆密码"    android:text="123456"    android:password="true"/>    <Button        android:id="@+id/btnLogin"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="GET方式登陆" />    <Button        android:id="@+id/btnQuery"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="POST方式查询" />    </LinearLayout>

package com.jxust.day06.entity;import java.io.Serializable;public class User implements Serializable{/** *  */private static final long serialVersionUID = 1L;private String name;private char sex;private int age;private double height;private String password;public String getName() {return name;}public void setName(String name) {this.name = name;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public User() {// TODO Auto-generated constructor stub}public User(String name, char sex, int age, double height, String password) {super();this.name = name;this.sex = sex;this.age = age;this.height = height;this.password = password;}@Overridepublic String toString() {return "User [name=" + name + ", sex=" + sex + ", age=" + age+ ", height=" + height + ", password=" + password + "]";}}

更多相关文章

  1. Android获取存储卡路径的方式
  2. 推荐几个比较好的android客户端开发教程
  3. 淘宝(阿里百川)手机客户端开发日记第一篇 android 主框架搭建(三
  4. android 使用gdb调试的方式
  5. android中设置activity的出现方式
  6. Android五种存储方式
  7. 新浪微博Android客户端学习记录四:完成OAuth认证

随机推荐

  1. 构建Android平台Google Map应用!
  2. Android高效率编码-第三方SDK详解系列(一
  3. Android(安卓)无入侵解决按钮重复点击---
  4. Android: SQLite + ListView 实现 新闻 A
  5. 美团 Android(安卓)热更新方案 Robust 开
  6. Android中XML的命名空间、自定义属性xmln
  7. Android(安卓)Studio上方便使用butterkni
  8. Android(安卓)UI设计初步(基本布局)
  9. 安卓音频采集播放方法
  10. Android(安卓)学习笔记四:创建工具栏按钮