//第一种/** * 获取参数(ArrayList nameValuePairs,String url)后post给远程服务器 * 将获得的返回结果(String)返回给调用者 * 本函数适用于查询数量较少的时候 * qian.long * 2011-06-07 */public String posturl(ArrayList nameValuePairs,String url) {    String result = "";    String tmp= "";    InputStream is = null;    try {        HttpClient httpclient = new DefaultHttpClient();        HttpPost httppost = new HttpPost(url);        httppost.setEntity(new UrlEncodedFormEntity(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(new InputStreamReader(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(int i = 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 * qian.long * 2011-06-07 */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(new InputStreamReader(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;} //第三种/** * 获取指定地址的网页数据 * 返回数据流 * qian.long * 2011-06-07 */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;        int responseCode = 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();    }    return inStream;}
 

更多相关文章

  1. C语言函数的递归(上)
  2. Android之SQLite数据库的使用
  3. Android应用程序安装过程源代码分析(2)
  4. android短信的接收和发送处理
  5. Android(安卓)Studio开发之ndk报错--error: no template named
  6. android中SurfaceView SurfaceHolder SurfaceHolder.Callback 关
  7. android—调试命令service
  8. Android4.4以太网移植问题
  9. android Audio调试程序常用命令

随机推荐

  1. fanfou(饭否) android客户端 代码学习1
  2. Android(安卓)OpenCV 灰度图转化
  3. Android使用HttpURLConnection显示网络图
  4. Android事件传递机制
  5. Android(安卓)Media Server - MediaPlaye
  6. Howto Build Android(安卓)KitKat (4.4)
  7. Android(安卓)一个简单的计算器APP
  8. Android与Webview交互
  9. android项目colors.xml中几个常用的颜色
  10. Android(安卓)一张图理解getWidth和getMe