綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] Exception after excuting HttpResponse

[複製連結] 查看: 1737|回覆: 1|好評: 0
跳轉到指定樓層
樓主
albert1014 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2011-11-29 00:18

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
Dear Madam/Sir,

I have some codes section as below :

-----

{



  HttpClient client = new DefaultHttpClient();
  StringBuilder builder = new StringBuilder();

  HttpGet myget = new HttpGet("http://test123abc.comoj.com/json_test.php");

  try {
      HttpResponse response = client.execute(myget);

      .....

  }

  catch (Exception e) {
      Log.v("url response", "false");
      e.printStackTrace();
  }

}

-----

Every time I run this codes with AVD under Eclipse, it will always drop into the catch function with exception.

I tried to debug, then found that the problem happened after executing the code "HttpResponse response = client.execute(myget)".

After I saw some suggestion on internet, I added the following code in the AndroidManifest.xml which is outside of the <application> section.

" <uses-permission android:name="android.permission.INTERNET" /> "

But the same exception happens.


I want to know if there is any other reason to cause this problem.

I think this should be the simple question but I am just the beginner in Android development.

So please help me with any suggestion or checking point.

I will appreciate your any help. Thank you very much.

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ploglin | 收聽TA | 只看該作者
發表於 2011-11-29 10:40
The following is my suggestion

  1.         public static String getInputStreamFromUrl(String remoteUrl, String chrset) {
  2.                 // TODO Auto-generated method stub

  3.                 isError = false;

  4.                 if (cacheData.containsKey(remoteUrl)) {
  5.                         Log.d(TAG, "Getting from cache file: " + remoteUrl);
  6.                         return cacheData.get(remoteUrl);
  7.                 }

  8.                 InputStream is = null;
  9.                 StringBuilder sb = new StringBuilder();
  10.                 HttpURLConnection connection;
  11.                 try {
  12.                         URL url = new URL(remoteUrl);
  13.                         Log.d(TAG, "fetch url " + remoteUrl);
  14.                         connection = (HttpURLConnection) url.openConnection();
  15.                         connection.setReadTimeout(30000);
  16.                         is = connection.getInputStream();
  17.                         BufferedReader reader = new BufferedReader(new InputStreamReader(is, chrset));
  18.                         sb = new StringBuilder();
  19.                         String line = null;
  20.                         while ((line = reader.readLine()) != null) {
  21.                                 sb.append(line + "\n");
  22.                         }
  23.                 } catch (SocketTimeoutException e) {
  24.                         Log.e(TAG, "SocketTimeoutException" + e.getMessage());
  25.                         isError = true;
  26.                 } catch (IOException e) {
  27.                         // TODO Auto-generated catch block
  28.                         Log.e(TAG, "IOException" + e.getMessage());
  29.                         isError = true;
  30.                 } finally {
  31.                         if (is != null) {
  32.                                 try {
  33.                                         is.close();
  34.                                 } catch (IOException e) {
  35.                                         // TODO Auto-generated catch block
  36.                                         e.printStackTrace();
  37.                                 }
  38.                         }
  39.                 }
  40.                 if (!sb.toString().equals("")) {
  41.                         cacheData.put(remoteUrl, sb.toString());
  42.                 }
  43.                 return sb.toString();
  44.         }

複製代碼
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則