Android根本Http基本通信
关于http通信的其实自己也不是很懂,就是略微知道一些基本的通信方法
方法1:
DefaultHttpClient httpclient = new DefaultHttpClient();// 创建http客户端
HttpGet httpget = new HttpGet(httpUrl);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();//得到http的内容
response.getStatusLine().getStatusCode();//得到http的状态返回值
result = EntityUtils.toString(response.getEntity());//得到具体的返回值,一般是xml文件
entity.consumeContent();//如果entity不为空,则释放内存空间
httpclient.getCookieStore();//得到cookis
httpclient.getConnectionManager().shutdown();//关闭http客户端
方法2:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(httpUrl);
httpclient.setCookieStore(DataDefine.mCookieStore);
HttpParams params = httpclient.getParams(); // 计算网络超时用
HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
HttpConnectionParams.setSoTimeout(params, 2 * 1000);
httpPost.setHeader("Content-Type", "text/xml");
StringEntity httpPostEntity = new StringEntity(base64, "UTF-8");//base64是经过编码的字符串,可以理解为字符串
//StringEntity httpPostEntity = new StringEntity("UTF-8");
httpPost.setEntity(httpPostEntity);
HttpResponse response = httpclient.execute(httpPost);