0

  1. The Code below on execution in android throws IOException but not wen executed as a java project.
  2. Also i am unable to view the exception in the console that has occurred if i use e.printstacktrace.

    Kindly reply as soon as possible

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) 

this the exception that occurs..

4
  • kindly check in the logcat not into the console from ddms Commented Aug 16, 2011 at 9:55
  • 2
    Have you provided with proper permissions? Commented Aug 16, 2011 at 9:55
  • Have you tried using sysout(e.toString()); Commented Aug 16, 2011 at 9:59
  • It's most likely a problem with the internet connection on the Android device. Try accessing exchangerate-api.com in the Android browser to verify that it's possible to reach the server from the device at all. Commented Jan 30, 2014 at 12:08

1 Answer 1

2

provide Internet permssion

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

And if you provided already then post your log cat

Try that below code

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; } 
Sign up to request clarification or add additional context in comments.

5 Comments

hmm..but your code looks ok.Check this url in your computer browser that is it working or not?.Sometime Unknown Host exceptioncame because Host is not responding to your request.So try to clean project
i checked it with the browser and also writing this as a simple java application and it works fine.. but with android it shows this exception
I think you have problem with url as my code is running for my url.Once put google.com in my code.It will work fine
String newUrl = "exchangerate-api.com/"+from+"/"+to+"/…"; i used it like this
if i just give String newURL="google.com" it throws malformedUrlException

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.