android http请求




import java.io.File;
import java.io.InputStream;
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.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.itcast.login.util.StreamTools;


public class LoginService {


/**
* 采用get的方式提交数据到服务器

* @param context
* @param name
* @param password
* @throws Exception
*/
public static void loginByHttpGet(Context context, String name,
String password) throws Exception {
// 1.提交中文的信息 必须对数据进行url编码
// 2.提交的信息 服务器端是采用iso-8859-1的编码集 获取的数据 手工的转码
String newname = URLEncoder.encode(name);
String newpassword = URLEncoder.encode(password);
String path = "http://192.168.1.250:8080/web/LoginServlet?name="
+ newname + "&password=" + newpassword;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
byte[] data = StreamTools.getBytes(is);
String message = new String(data, "gbk");
Toast.makeText(context, message, 0).show();
} else {
Toast.makeText(context, "访问服务器异常", 0).show();
}


}


/**
* 通过post的方式提交数据

* @param context
* @param name
* @param password
* @throws Exception
*/
public static void loginByHttpPost(Context context, String name,
String password) throws Exception {
// 1.提交数据的服务器地址
String path = "http://192.168.1.250:8080/web/LoginServlet";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
// 通过post的方式 向服务器提交数据 必须要设置 content-type content-length
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
String newname = URLEncoder.encode(name);
String newpassword = URLEncoder.encode(password);
// name=zhangsan&password=12323
String data = "name=" + newname + "&password=" + newpassword; // 组拼出来的要提交的数据
conn.setRequestProperty("Content-Length", data.length() + "");
// 设置 允许向服务器发送数据
conn.setDoOutput(true);
// 获取到向服务器发送数据的输出流
OutputStream os = conn.getOutputStream();
// 向服务器写出数据
os.write(data.getBytes());
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
byte[] result = StreamTools.getBytes(is);
Toast.makeText(context, new String(result), 0).show();
} else {
Toast.makeText(context, "服务内部错误", 0).show();
}
}


/**
* 采用httpclient 发送一个get请求

* @param context
* @param name
* @param password
* @throws Exception
*/
public static void loginByHttpClientGet(Context context, String name,
String password) throws Exception {
// 1. 打开一个浏览器 .
DefaultHttpClient client = new DefaultHttpClient();
// 2. 准备一个http的get请求.
String newname = URLEncoder.encode(name);
String newpassword = URLEncoder.encode(password);
String path = "http://192.168.1.250:8080/web/LoginServlet?name="
+ newname + "&password=" + newpassword;
HttpGet httpGet = new HttpGet(path);
// 3. 敲回车
HttpResponse response = client.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();
byte[] result = StreamTools.getBytes(is);
Toast.makeText(context, new String(result, "gbk"), 0).show();
} else {
Toast.makeText(context, "服务内部错误", 0).show();
}
}


/**
* 采用httpclient post一条数据到服务器

* @param context
* @param name
* @param password
* @throws Exception
*/
public static void loginByHttpClientPost(Context context, String name,
String password) throws Exception {
// 1. 打开一个浏览器 .
DefaultHttpClient client = new DefaultHttpClient();


// 2.准备一个http的post请求
String path = "http://192.168.1.250:8080/web/LoginServlet";
HttpPost httpPost = new HttpPost(path);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
// name=zhangsan&password=12323
parameters.add(new BasicNameValuePair("name", name));
parameters.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));


// 3.敲回车
HttpResponse response = client.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();
byte[] result = StreamTools.getBytes(is);
Toast.makeText(context, new String(result), 0).show();
} else {
Toast.makeText(context, "服务内部错误", 0).show();
}
}


/**
* 上传文件到服务器

* @param file
*/
public static void uploadFile(Context context, File file) {
// 创建一个httppost的请求
PostMethod filePost = new PostMethod(
"http://192.168.1.250:8080/web/UploadFileServlet");
try {
// 组拼上传的数据
Part[] parts = { new StringPart("source", "695132533"),
new StringPart("status", "lisi"),
new FilePart("file", file) };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == 200) {
Toast.makeText(context, "上传文件成功", 0).show();
}
} catch (Exception e) {
Toast.makeText(context, "上传文件失败", 0).show();
}


finally {
filePost.releaseConnection();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值