Android 台灣中文網

標題: Exception after excuting HttpResponse [打印本頁]

作者: albert1014    時間: 2011-11-29 00:18
標題: Exception after excuting HttpResponse
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.


作者: ploglin    時間: 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 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1