最近一段时间一直在做一个项目,学习Java才只有短短的9个月,但是感觉自己做android的这一段时间是进步最快的,学习进度也比以前快了很多,今天刚好有时间,就记录一下自己在android开发中遇到的各个问题。
1>/android端与服务器端的连接问题,用的协议是Http协议,android的提供的类是HttpClient,相当与一个浏览器一样(类似)。
public class HttpUtil{
// 获得Get请求对象request
public static HttpGet getHttpGet(String url){
HttpGet request = new HttpGet(url);
return request;
}
// 获得Post请求对象request
public static HttpPost getHttpPost(String url){
HttpPost request = new HttpPost(url);
return request;
}
/**
* httpclient 浏览器的简单包装
* new HttpClient 就相当于得到了一个浏览器
*/
public static String sendDataByGet (String path,String username,String password,String area) throws Exception{
//1. 获取到一个浏览器的实例
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // 设置连接超时时间
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); // 设置接受返回值超时时间
//2. 准备请求的地址
//HttpGet httpGet = new HttpGet(path);
HttpGet httpGet=HttpUtil.getHttpGet(path+"?"+"username="+username+"&"+"password="+password+"&"+"area="+area);
URLEncoder.encode(area, "UTF-8");
URLEncoder.encode(username, "UTF-8");
//3. 敲回车 发请求
HttpResponse response = client.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
if(code == 200){
InputStream is =response.getEntity().getContent();
String result=inStream2String(is);
return result;
}
else{
throw new IllegalStateException("服务器状态异常");
}
}
//登陆注册的方法
public static String sendDataByPost(String path,String username,String password) throws Exception{
//1.获取到一个浏览器实例
HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // 设置连接超时时间
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); // 设置接受返回值超时时间
//2.准备请求的数据类型
// HttpPost httpPost=new HttpPost(path);
HttpPost httpPost=HttpUtil.getHttpPost(path);
//键值对
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",username));
params.add(new BasicNameValuePair("password",password));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
//3.设置post请求的数据实体
httpPost.setEntity(entity);
//4.发送数据给服务器
HttpResponse response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
if(code==200){
InputStream is=response.getEntity().getContent();
String result=inStream2String(is);
return result;
}else{
throw new IllegalStateException("服务器状态异常");
}
}
/*
* 取件 访问服务器方式
* @param path 相对路径
* @return result
*/
public static String pickUpByPost(String path){
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); // 设置连接超时时间
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); // 设置接受返回值超时时间
HttpGet httpGet = new HttpGet();
HttpResponse response;
String result = null;
try {
URLEncoder.encode(path, "UTF-8");
httpGet.setURI(new URI(Constants.SERVERPATH + path));
response = client.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(),"UTF-8");
} else {
Log.i("info", "网络问题,发送失败!");
result=null;
//throw new IllegalStateException("服务器状态异常");
}
client.getConnectionManager().shutdown();
} catch (URISyntaxException e1) {
e1.printStackTrace();
client.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
client.getConnectionManager().shutdown();
} catch (IOException e) {
e.printStackTrace();
client.getConnectionManager().shutdown();
}
return result;
}
//将输入流转换为输出流
private static String inStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buf=new byte[1024];
int len=-1;
while((len=is.read(buf))!=-1){
baos.write(buf,0,len);
}
return new String(baos.toByteArray());
}
}
第一次写这个 代码写的比较乱,但是主要的方法都写了 都能实现,
其中后来碰到一个自己比较模糊的知识点(get和Post的区别),在将json格式字符串发送给服务器端时,get方式报非法字符的错误,不能含有{},于是该用post方法,以前只知道get发送的数据量小,post能够发送比较大的数据量,造成编码的错误,get方法的url地址中信息显示出来,但是post的信息是被隐藏起来的,所以写地址路径的时候应该分清楚。。新学android,只是为了回忆自己所学的。有错误,请批评指正。谢谢!!!