标签:android blog http java os 数据
package cn.xm.hostrequest.biz;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
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.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 android.content.Context;
import android.widget.Toast;
import cn.xm.hostrequest.tools.StreamTools;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class LoginService {
public static boolean loginByGet(String path, String username,
String password) {
try {
URL url = new URL(path + "?user="
+ URLEncoder.encode(username, "utf-8") + "&pwd="
+ URLEncoder.encode(password, "utf-8"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
String response = StreamTools.readInputInputStream(conn
.getInputStream());
if (response != null && response.equals("true")) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean loginByPost(String path, String username,
String password) {
try {
// 创建 url
URL url = new URL(path);
// 利用 url打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置 请求参数
conn.setRequestMethod("POST"); // post 请求
conn.setConnectTimeout(5000); // 连接超时时间
conn.setDoOutput(true); // 充许写出输出流到 服务器
// 拼装 post请求的数据
String data = "user=" + URLEncoder.encode(username, "utf-8")
+ "&pwd=" + URLEncoder.encode(password, "utf-8");
// 设置 消息头
conn.addRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 设置数据长度
conn.addRequestProperty("Content-Length", data.length() + "");
// 取输出流 并写入数据
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
// 执行请求 并取响应码
int code = conn.getResponseCode();
if (code == 200) {
String response = StreamTools.readInputInputStream(conn
.getInputStream());
if (response != null && response.equals("true")) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean loginByClientPost(String path, String username,
String password) {
try {
// 打开 httpClient == 打开浏览器
HttpClient client = new DefaultHttpClient();
// 设置 要 访问的 url == form 中的 action
HttpPost post = new HttpPost(path);
// 创建 post 数据 == form 中填写的数据
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("user", username));
parameters.add(new BasicNameValuePair("pwd", password));
// 设置数据
post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
// 发送请求并得到响应
HttpResponse response = client.execute(post);
// 获取响应码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
String text = StreamTools.readInputInputStream(response
.getEntity().getContent());
if (text != null && text.equals("true")) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean loginByClientGet(String path, String username,
String password) {
try {
// 打开httpclient==打开浏览器
HttpClient client = new DefaultHttpClient();
// 生成 要访问的 url == 输入地址
HttpGet httpGet = new HttpGet(path);
// 发送请求 == 敲回车
HttpResponse response = client.execute(httpGet);
// 取状态码
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
String text = StreamTools.readInputInputStream(response
.getEntity().getContent());
if (text != null && text.equals("true")) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void loginByAsyncGet(final Context context, String path,
String username, String password) {
AsyncHttpClient client = new AsyncHttpClient();
client.get(path + "?user=" + username + "&pwd=" + password,
new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
String text = new String(responseBody);
if ("true".equals(text)) {
Toast.makeText(context, "登陆成功", 0).show();
} else {
Toast.makeText(context, "登陆失败", 0).show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(context, "登陆失败", 0).show();
}
});
}
public static void loginByAsyncPost(final Context context, String path,
String username, String password) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams parm = new RequestParams();
parm.put("user", username);
parm.put("pwd", password);
client.post(path, parm, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
String text = new String(responseBody);
if ("true".equals(text)) {
Toast.makeText(context, "登陆成功", 0).show();
} else {
Toast.makeText(context, "登陆失败", 0).show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(context, "登陆失败", 0).show();
}
});
}
}
标签:android blog http java os 数据
原文地址:http://www.cnblogs.com/acecssy/p/3847447.html