数据小于2k时用get,大于时候用post,
指定实体数据内容类型
将信息打包成Map对象
public class NewsService {
public static Boolean save(String title, String length) {
String path = "http//192.168.1.100";
Map<String,String> params = new HashMap<String, String>();
params.put("title", title);
params.put("timelength", length);
try {
return sendGETRequest(path,params,"UTF-8");//采用utf—8解决中文乱码问题
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
用get方式将该信息提交给map对象,中文会乱码,将值用UTF-8编码,服务器中也编码,或者在服务器中使用过滤器
/**
* 发送GET请求
* @param path 请求路径
* @param params 请求参数
* @return 请求是否成功
* @throws Exception
*/
private static Boolean sendGETRequest(String path,Map<String, String> params,String encoding) //异常抛到上一层捕获处理
throws Exception{
//http://192.168.1.100?title=xxxx&timelength=90
StringBuilder url = new StringBuilder(path);
url.append("?");
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry: params.entrySet()){
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(),encoding));//解决中文乱码问题
url.append("&");
}
<span style="font-family: Arial, Helvetica, sans-serif;">url.deleteCharAt(url.length()-1);</span>
}
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() ==200){
return true;
}
return false;
}
使用post方式上传参数,下图是网页提交参数给TomCat服务器的参数,左侧是客户端提交的http协议,右侧是服务器返回的响应码
private static Boolean sendPOSTRequest(String path,Map<String, String> params,String encoding) //异常抛到上一层捕获处理
throws Exception{
//http://192.168.1.100?title=xxxx&timelength=90 路径写为此格式
StringBuilder url = new StringBuilder(path);
url.append("?");
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry: params.entrySet()){
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(),encoding));//解决中文乱码问题
url.append("&");
}
url.deleteCharAt(url.length()-1);
}
byte[] entity = path.toString().getBytes(); //生成实体数据
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true); //允许对外输出数据
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",String.valueOf(entity.length));
OutputStream os = conn.getOutputStream();
os.write(entity);
if(conn.getResponseCode() ==200){
return true;
}
return false;
}
使用HttpClient提交参数,不需要额外jar文件,该开源项目已经嵌到android中。client也可以完成浏览器的部分功能
private boolean sendHttpClientPOSTRequest(String path,Map<String,String> params,String encoding) throws Exception{
List<NameValuePair> pairs = new ArrayList<NameValuePair>(); //存放请求参数
if(params !=null && !params.isEmpty()){
for(Map.Entry<String, String> entry: params.entrySet()){
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}}
// title=liming&timelength=90
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,encoding);
HttpPost httpPost=new HttpPost(path);
httpPost.setEntity(entity);
DefaultHttpClient client = new DefaultHttpClient();
client.execute(httpPost);
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() ==200){
return true;
}
return false;
}