/**获取参数(ArrayList<NameValuePair> nameValuePairs,String url)后 *  post给远程服务器  *  将获得的返回结果(String)返回给调用者  *  本函数适用于查询数量较少的时候  *  Chen.Zhidong  *  2011-02-15 */ 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(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  *  Chen.Zhidong  *  2011-02-15 */ 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; } 

/**获取指定地址的网页数据  *  返回数据流  *  Chen.Zhidong  *  2011-02-18 */ 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版本更新实例详解
  3. A31S-camera流程
  4. Android下使用c++11的测试
  5. Android(安卓)使用JAVA代码控制UI界面
  6. tess_two Android图片文字识别
  7. Android(安卓)onBackPressed()函数重载失效问题
  8. android 动态改变控件位置和大小
  9. 如何刷新viewpager中的listfragment?

随机推荐

  1. android中IO操作数据总结
  2. Android在自己的程序中调用外部程序
  3. android 记得用mkdirs不要用mkdir
  4. android 拖拽图片&拖动浮动按钮到处跑
  5. Android -- EditText插入表情
  6. CheckBox自定义样式
  7. Android之Audio常用属性变量
  8. 【Android】播放视频的简易播放器源码
  9. Google Android之使用XML布局
  10. Installing the Eclipse Plugin