2011-08-16 86 views
0

Android的異常處理

  1. 低於上執行的代碼的Android拋出IOException異常但不作爲文Java項目執行。
  2. 另外我無法查看控制檯中發生的異常,如果我使用e.printstacktrace。

    請儘快回覆可能

public String convert(String from, String to,int amt){ 
    String result= null; 
    URL url = null; 
    HttpURLConnection urlConnection = null; 
    try { 
     url = new URL("http://www.exchangerate-api.com/"+from+"/"+to+"/"+amt+"?k=ZTKOy-28yPI-g2cQd"); 
     System.out.println("hai"); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
     result = readStream(in); 
     System.out.println(result); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 
public static String readStream(InputStream in) throws IOException { 
    StringBuilder sb = new StringBuilder(); 
    BufferedReader r = new BufferedReader(new InputStreamReader(in),1000); 
    for (String line = r.readLine(); line != null; line =r.readLine()){ 
     sb.append(line); 
    } 
    in.close(); 
    return sb.toString(); 
} 

08-17 12:30:31.356: WARN/System.err(946): java.net.UnknownHostException: www.exchangerate-api.com 
08-17 12:30:31.416: WARN/System.err(946):  at java.net.InetAddress.lookupHostByName(InetAddress.java:506) 
08-17 12:30:31.447: WARN/System.err(946):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 
08-17 12:30:31.468: WARN/System.err(946):  at java.net.InetAddress.getAllByName(InetAddress.java:256) 
08-17 12:30:31.496: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:68) 
08-17 12:30:31.517: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
08-17 12:30:31.537: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:298) 
08-17 12:30:31.557: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
08-17 12:30:31.586: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 
08-17 12:30:31.606: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 
08-17 12:30:31.626: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018) 
08-17 12:30:31.649: WARN/System.err(946):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:510) 
08-17 12:30:31.679: WARN/System.err(946):  at com.CurrencyConverterActivity.ConverterCurrency.convert(ConverterCurrency.java:110) 
08-17 12:30:31.697: WARN/System.err(946):  at com.CurrencyConverterActivity.ConverterCurrency.onClick(ConverterCurrency.java:67) 
08-17 12:30:31.716: WARN/System.err(946):  at android.view.View.performClick(View.java:2485) 
08-17 12:30:31.736: WARN/System.err(946):  at android.view.View$PerformClick.run(View.java:9080) 
08-17 12:30:31.746: WARN/System.err(946):  at android.os.Handler.handleCallback(Handler.java:587) 
08-17 12:30:31.766: WARN/System.err(946):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-17 12:30:31.786: WARN/System.err(946):  at android.os.Looper.loop(Looper.java:123) 
08-17 12:30:31.806: WARN/System.err(946):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
08-17 12:30:31.826: WARN/System.err(946):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-17 12:30:31.836: WARN/System.err(946):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-17 12:30:31.856: WARN/System.err(946):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-17 12:30:31.876: WARN/System.err(946):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-17 12:30:31.896: WARN/System.err(946):  at dalvik.system.NativeStart.main(Native Method) 

發生異常..

+0

查收在logcat中不進從DDMS控制檯 – Pratik

+2

你提供了適當的權限嗎? –

+0

您是否嘗試過使用sysout(e.toString()); –

回答

2

提供互聯網PERMSSION

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

如果您提供的已經然後發佈日誌貓

試一下下面的代碼

HttpsURLConnection con = (HttpsURLConnection) new URL(newUrl).openConnection(); 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      con.setRequestProperty("Content-Length", ""+Integer.toString(newUrl.getBytes().length)); 
      con.setRequestProperty("Content-Language", "en-US"); 
      con.setRequestProperty("Connection", "close"); 
      con.setUseCaches (false); 
      con.setDoOutput(true); 
      con.setDoInput(true); 
      InputStream in =con.getInputStream(); 
      InputStreamReader is=new InputStreamReader(in); 
      BufferedReader br=new BufferedReader(is); 
      String read=br.readLine(); 
      String line; 
       while((line=br.readLine())!=null){ 
       System.out.println(read); 
       read+=line; 
      } 
+0

nope它沒有工作 – Britto

+0

hmm ..但你的代碼看起來ok.Check在計算機瀏覽器中的這個URL是否正常工作?。有時候未知的主機exceptioncame,因爲主機沒有響應您的請求。所以嘗試清理項目 – Sameer

+0

我檢查了它與瀏覽器,也寫這是一個簡單的Java應用程序,它工作正常..但與Android它顯示這個異常 – Britto