- 低於上執行的代碼的Android拋出IOException異常但不作爲文Java項目執行。
- 另外我無法查看控制檯中發生的異常,如果我使用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)
這發生異常..
查收在logcat中不進從DDMS控制檯 – Pratik
你提供了適當的權限嗎? –
您是否嘗試過使用sysout(e.toString()); –