简单的Android服务端和客户端登录交互(客户端)
2014-04-09 15:11
232 查看
HttpClientToServer.java
package com.bao;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientToServer {
String urlAddress = "http://10.0.2.2:8080/AndroidServer/login.do";
public HttpClientToServer(){
}
public String doGet(String username,String password){
String getUrl = urlAddress + "?username="+username+"&password="+password;
HttpGet httpGet = new HttpGet(getUrl);
HttpClient hc = new DefaultHttpClient();
try {
HttpResponse ht = hc.execute(httpGet);
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity he = ht.getEntity();
InputStream is = he.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
//String str = EntityUtils.toString(he);
System.out.println("========="+response);
return response;
}else{
return "error";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
}
public String doPost(String username,String password){
//String getUrl = urlAddress + "?username="+username+"&password="+password;
HttpPost httpPost = new HttpPost(urlAddress);
List params = new ArrayList();
NameValuePair pair1 = new BasicNameValuePair("username", username);
NameValuePair pair2 = new BasicNameValuePair("password", password);
params.add(pair1);
params.add(pair2);
HttpEntity he;
try {
he = new UrlEncodedFormEntity(params, "gbk");
httpPost.setEntity(he);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpClient hc = new DefaultHttpClient();
try {
HttpResponse ht = hc.execute(httpPost);
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity het = ht.getEntity();
InputStream is = het.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
//String str = EntityUtils.toString(he);
System.out.println("========="+response);
return response;
}else{
return "error";
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
}
}
LoginActivity.java
package com.bao;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
private EditText username;
private EditText password;
private Button login;
private Button reset;
UserManager userManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
login = (Button)findViewById(R.id.login);
reset = (Button)findViewById(R.id.reset);
userManager = new UserManager();
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
boolean isSuccess = userManager.isRightUser(username.getText().toString(), password.getText().toString());
if(isSuccess == true){
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LoginActivity.this, "密码或用户名错误", Toast.LENGTH_LONG).show();
}
}
});
reset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
username.setText("");
password.setText("");
}
});
}
}UrlConnectionToServer.java
package com.bao;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class UrlConnectionToServer {
String urlAddress = "http://10.0.2.2:8080/AndroidServer/login.do";
URL url;
HttpURLConnection uRLConnection;
public UrlConnectionToServer(){
}
public String doGet(String username,String password){
String getUrl = urlAddress + "?username="+username+"&password="+password;
try {
url = new URL(getUrl);
uRLConnection = (HttpURLConnection)url.openConnection();
InputStream is = uRLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
uRLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String doPost(String username,String password){
try {
url = new URL(urlAddress);
uRLConnection = (HttpURLConnection)url.openConnection();
uRLConnection.setDoInput(true);
uRLConnection.setDoOutput(true);
uRLConnection.setRequestMethod("POST");
uRLConnection.setUseCaches(false);
uRLConnection.setInstanceFollowRedirects(false);
uRLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uRLConnection.connect();
DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream());
String content = "username="+username+"&password="+password;
out.writeBytes(content);
out.flush();
out.close();
InputStream is = uRLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = "";
String readLine = null;
while((readLine =br.readLine()) != null){
//response = br.readLine();
response = response + readLine;
}
is.close();
br.close();
uRLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
UserManager.java
package com.bao;
public class UserManager {
UrlConnectionToServer urlConnectionToServer = new UrlConnectionToServer();
HttpClientToServer httpClientToServer = new HttpClientToServer();
public boolean isRightUser(String username,String password){
String response = httpClientToServer.doPost(username, password);
if("true".equals(response)){
return true;
}else{
return false;
}
}
}
main.xml
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="用户登陆"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:gravity="right"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/username"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:gravity="right"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText android:password="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/password"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button android:layout_marginLeft="160px"
android:id="@+id/login"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="登陆"/>
<Button android:id="@+id/reset"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="重置"/>
</LinearLayout>
</LinearLayout>
相关文章推荐
- 简单的Android服务端和客户端登录交互(服务端)
- Android客户端配合Servlert服务端实现简单的数据交互
- android客户端和servlet服务端的简单登录实现
- 简单的android客户端servlet服务端的交互
- 简单的android客户端servlet服务端的交互
- Android客户端与服务端交互之登陆示例
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互(转)
- 简单java Web Service服务端和Android客户端
- Android客户端和php+mysql+apache搭建的服务器之间的简单交互
- android开发步步为营之6:客户端通过tcp和服务端进行交互
- Android 客户端与服务器端进行数据交互(二、登录客户端)
- 搭建与Android 交互的简单的网络服务端
- 【Android 开发】: Android客户端与服务端之间使用GSON交互数据。
- android客户端与服务端交互的三种方式
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android---网络交互之客户端请求服务端资源
- JavaServlet服务端与客户端简单交互
- Android客户端与服务器交互,登录案例
- Android网络编程之TCP通信----简单客户端与服务端通信
- android开发步步为营之4:客户端通过http和服务端进行交互