Android获取网页数据的方法总结
2014-09-09 16:49
435 查看
本文总结了三种获取网页数据的代码。
/**************** 第一种
****************/
/* 获取参数 (ArrayList<NameValuePair> nameValuePairs,String url) 后post给远程服务器
* 将获得的返回结果(String)返回给调用者
* 本函数适用于查询数量较少的时候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
String result ="";
String tmp="";
InputStream is =null;
try{
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost =new HttpPost(url);
httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return"Fail to establish http connection!";
}
try{
BufferedReader reader =new BufferedReader(newInputStreamReader(is,"utf-8"));
StringBuilder sb =new StringBuilder();
String line =null;
while((line = reader.readLine()) != null) {
sb.append(line +"\n");
}
is.close();
tmp=sb.toString();
}catch(Exception e){
return"Fail to convert net stream!";
}
try{
JSONArray jArray =new JSONArray(tmp);
for(inti=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Iterator<?> keys=json_data.keys();
while(keys.hasNext()){
result += json_data.getString(keys.next().toString());
}
}
}catch(JSONException e){
return"The URL you post is wrong!";
}
return result;
}
/*********************** 第二种 *********************/
/* 获取参数指定的网页代码,将其返回给调用者,由调用者对其解析
* 返回 String
*/
public String posturl(String url){
InputStream is =null;
String result ="";
try{
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost =new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return"Fail to establish http connection!"+e.toString();
}
try{
BufferedReader reader =new BufferedReader(newInputStreamReader(is,"utf-8"));
StringBuilder sb =new StringBuilder();
String line =null;
while((line = reader.readLine()) != null) {
sb.append(line +"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
return"Fail to convert net stream!";
}
return result;
}
/*************** 第三种 *****************/
/** 获取指定地址的网页数据
* 返回数据流
*/
public InputStream streampost(String remote_addr){
URL infoUrl =null;
InputStream inStream =null;
try{
infoUrl =new URL(remote_addr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
intresponseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
inStream = httpConnection.getInputStream();
}
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returninStream;
}
摘自 虚怀若谷
/**************** 第一种
****************/
/* 获取参数 (ArrayList<NameValuePair> nameValuePairs,String url) 后post给远程服务器
* 将获得的返回结果(String)返回给调用者
* 本函数适用于查询数量较少的时候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
String result ="";
String tmp="";
InputStream is =null;
try{
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost =new HttpPost(url);
httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return"Fail to establish http connection!";
}
try{
BufferedReader reader =new BufferedReader(newInputStreamReader(is,"utf-8"));
StringBuilder sb =new StringBuilder();
String line =null;
while((line = reader.readLine()) != null) {
sb.append(line +"\n");
}
is.close();
tmp=sb.toString();
}catch(Exception e){
return"Fail to convert net stream!";
}
try{
JSONArray jArray =new JSONArray(tmp);
for(inti=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Iterator<?> keys=json_data.keys();
while(keys.hasNext()){
result += json_data.getString(keys.next().toString());
}
}
}catch(JSONException e){
return"The URL you post is wrong!";
}
return result;
}
/*********************** 第二种 *********************/
/* 获取参数指定的网页代码,将其返回给调用者,由调用者对其解析
* 返回 String
*/
public String posturl(String url){
InputStream is =null;
String result ="";
try{
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost =new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
return"Fail to establish http connection!"+e.toString();
}
try{
BufferedReader reader =new BufferedReader(newInputStreamReader(is,"utf-8"));
StringBuilder sb =new StringBuilder();
String line =null;
while((line = reader.readLine()) != null) {
sb.append(line +"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
return"Fail to convert net stream!";
}
return result;
}
/*************** 第三种 *****************/
/** 获取指定地址的网页数据
* 返回数据流
*/
public InputStream streampost(String remote_addr){
URL infoUrl =null;
InputStream inStream =null;
try{
infoUrl =new URL(remote_addr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
intresponseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
inStream = httpConnection.getInputStream();
}
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returninStream;
}
摘自 虚怀若谷
相关文章推荐
- android初级之Android获取网页数据的方法总结
- 【android初级】之Android获取网页数据的方法总结
- 转载 Android获取网页数据的方法总结
- Android获取网页数据的方法总结
- android初级之Android获取网页数据的方法总结
- Android获取网页数据的方法总结
- android获取网页数据的方法
- Qt获取网页数据方法与经验总结
- Android中获取网页表单中的数据
- php获取网页内容方法总结
- php获取网页内容方法总结
- Android应用程序四种数据存取方法总结
- [Android 中级] AsyncTask 网页数据获取并显示进程
- 转:php获取网页内容方法总结
- Android中Activity之间数据传递的几种方法总结
- 利用JavaScript从UIWebView获取、修改、提交网页内数据的方法
- php获取网页内容方法总结发布
- (转)Android从服务器端获取数据的几种方法
- 总结:VC#中从Clipboard保存获取数据的方法
- 【android初级】之Android之从网络上获取数据(图片,网页,xml,json等)