使用开源框架android-async-http-master 来实现模拟用户登录
选准备android-async-http-master 可以在https://github.com/loopj/android-async-http下载
并将里面的android-async-http-master\library\src\main\java下面的包拷到自已工程里面
开源的力量将在下面体现了,几行代码就能实现前两篇的功能,简单易懂
package com.lmjssjj.login;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
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 com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_name;
private EditText et_pwd;
// private String path="http://192.168.1.96:8080/Loginfromphone/login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_pwd = (EditText) findViewById(R.id.et_pwd);
}
public void login(View v) {
final String name = et_name.getText().toString().trim();
final String pwd = et_pwd.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
Toast.makeText(this, "用户名或密码不能为空", 0).show();
return;
}
String path = "http://192.168.1.96:8080/Loginfromphone/login?";
try {
path = path + "username=" + URLEncoder.encode(name, "utf-8")
+ "&password=" + URLEncoder.encode(pwd, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
client.get(path, new AsyncHttpResponseHandler() {
<span style="white-space:pre"> </span>//请求成功时的回调
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
Toast.makeText(MainActivity.this, new String(responseBody), 0)
.show();
}
<span style="white-space:pre"> </span>//错误时的回调
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, new String(responseBody), 0)
.show();
}
});
}
public void login1(View v) {
final String name = et_name.getText().toString().trim();
final String pwd = et_pwd.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
Toast.makeText(this, "用户名或密码不能为空", 0).show();
return;
}
String path = "http://192.168.1.96:8080/Loginfromphone/login";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("username", name);
params.add("password", pwd);
client.post(path, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
Toast.makeText(MainActivity.this, new String(responseBody), 0)
.show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(MainActivity.this, new String(responseBody), 0)
.show();
}
});
}
}