码迷,mamicode.com
首页 > 其他好文 > 详细

从远程服务器获取数据

时间:2015-10-07 10:54:08      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

package com.utils;

import java.io.IOException;

 

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

 

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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 org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TestHttp {

 
 
 public static String HttpGet(String url) throws ClientProtocolException, IOException{
  
  
  HttpClient httpClient=new DefaultHttpClient();
  HttpGet httpGet=new HttpGet(url);
  HttpResponse httpResponse= httpClient.execute(httpGet);
  
  

  return getResult(httpResponse);
  
 }
 
 
 
 
 
 
 public static String getResult(HttpResponse response) throws ParseException, IOException{
  String result="";
  //如果发送成功...
  if(response.getStatusLine().getStatusCode()==200){
  
   result=EntityUtils.toString(response.getEntity()); 
   return result;
  }else{
   return "服务器响应失败....TestHttp";
  }
 }
 
 
 public static JSONArray JsonHttpGet(String url) throws ParseException, IOException, JSONException{
  
  
  HttpClient httpClient=new DefaultHttpClient();
  HttpGet httpGet=new HttpGet(url);
  HttpResponse response= httpClient.execute(httpGet);
  
  
  JSONArray json = null;
  //如果发送成功...
  if(response.getStatusLine().getStatusCode()==200){
  
   //result=EntityUtils.toString(response.getEntity()); 
   
   HttpEntity entity = response.getEntity();
   InputStream is = entity.getContent();
   
   
   byte[] bytes = new byte[256];
   StringBuffer sb = new StringBuffer();
   while (is.read(bytes) > 0) {
    sb.append(new String(bytes));
    bytes = new byte[256];
   }
   
   
   //new JSONArray(sb.toString())
    json = new JSONArray(sb.toString());
   //json = JSONObject.fromObject(sb.toString());
  

    return json;
   
  }else{
   
   return null;
  }
 }
 
 
 
 
 
 
 /**
  * 模拟url访问 从特定的url中获取json
  *
  * @param urlStr
  * @param params
  * @return json object ,or null if failed
  * 参数经过封装后传过来 ,提交为 post请求
  */
 private static JSONObject getJsonFromUrl(String urlStr,
   Map<String, String> params) {
  HttpClient httpClient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost(urlStr);
  JSONObject json = null;
  try {
   if (params != null) {
    Iterator<String> keys = params.keySet().iterator();
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    while (keys.hasNext()) {
     String key = keys.next();
     nvps.add(new BasicNameValuePair(key, params.get(key)));
    }
    
    
    httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
   }
   HttpResponse response = httpClient.execute(httpPost);
   
   
   HttpEntity entity = response.getEntity();
   InputStream is = entity.getContent();
   
   
   byte[] bytes = new byte[256];
   StringBuffer sb = new StringBuffer();
   while (is.read(bytes) > 0) {
    sb.append(new String(bytes));
    bytes = new byte[256];
   }
   //json ="";//JSONObject.fromObject(sb.toString());
  } catch (Exception e) {
    e.printStackTrace();
  }

  return json;
 }

}

从远程服务器获取数据

标签:

原文地址:http://my.oschina.net/u/243698/blog/513873

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!