本帖最後由 ploglin 於 2011-10-26 14:33 編輯
我也分享我的作法,但我沒有把功能獨立出來- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://xxx.xxx.com.tw/xxxx/xxxxb.php");
- List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- nameValuePairs.add(new BasicNameValuePair("a", "a"));
- nameValuePairs.add(new BasicNameValuePair("b", "b"));
- try {
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
- httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0) Gecko/20100101 Firefox/7.0");
- httppost.addHeader("Referer", "http://xxx.xxx.com.tw/xxxx/xxxxa.php");
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity resEntity = response.getEntity();
- StringBuilder sb = new StringBuilder();
- if (resEntity != null) {
- InputStream is = resEntity.getContent();
- BufferedReader reader = new BufferedReader(new InputStreamReader(is, "big5"));
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line);
- }
- }
- Bundle bundle = new Bundle();
- bundle.putString("data", sb.toString());
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- }
複製代碼 |