这是一个简单实现了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客户端学习记录四:完成OAuth认证
  4. Php与Android(安卓)socket通信
  5. 淘宝(阿里百川)手机客户端开发日记第一篇 android 主框架搭建(三
  6. android 使用gdb调试的方式
  7. fullScreen时的软键盘监听(非重写Layout方式)
  8. android hessian
  9. android中设置activity的出现方式

随机推荐

  1. Android中JNI 的一些常用说明 JNI_OnLoad
  2. Android(java)学习笔记125:Clock app编写报
  3. android xml 分析1--- AndroidManifest.x
  4. Android 关闭线程(转)
  5. android Supporting multiple screen翻译
  6. android中调用系统功能 来显示本地相册图
  7. android小功能实现之发送短信
  8. 【Android 应用开发】Ubuntu 下 Android
  9. android的设备永不休眠(增加 Settings-->D
  10. Android中onCreateOptionsMenu(Menu menu