-1

I have the following java code

try {
    String u = "http://webapi.com/demo.zip";
    URL url = new URL(u);
    URLConnection ucon = url.openConnection();
    InputStream is = ucon.getInputStream();
}
catch (Exception e) {
    Log.d('downloaderror', e.getMessage());
}

But for some reason, the InputStream is = ucon.getInputStream() causes an error and the catch block is fired. And when the catch block is fired, the value of e is null.

Does anyone konw what is wrong with my ucon.getInputStream() ? I know for a fact that http://webapi.com/demo.zip exists, because I'm able to download the file from my web browser.

EDIT Here's the stack trace on ucon.getInputstream()

java.lang.Exception
            at com.example.instantramenz.samplewebview.MainActivity$1.onClick(MainActivity.java:94)
            at android.view.View.performClick(View.java:4633)
            at android.view.View$PerformClick.run(View.java:19270)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
John
  • 32,403
  • 80
  • 251
  • 422
  • The value of `e` is `null` or the String printed contains the text `null` (ie. the message is `null`)? – Sotirios Delimanolis Jul 19 '15 at 19:36
  • the value of e is null. And then null.getMessage() causes the app to crash after – John Jul 19 '15 at 19:37
  • Please post the stack trace. If I remember correctly, exceptions in a catch-block can never be `null`. – Brian Jul 19 '15 at 19:38
  • I get a 404 and text/html. Can you use Apache HttpClient? – Elliott Frisch Jul 19 '15 at 19:41
  • @ElliottFrisch the actual url is somethign that can only be accessed internally on my own network. But I can confirm that the file actually exists. – John Jul 19 '15 at 19:43
  • If you use Java 7+, use a try-with-resources statement! – fge Jul 19 '15 at 19:43
  • @SotiriosDelimanolis - the question you marked at duplicate doesn't seem to discuss the issue I'm having with getInputStream(). Can you refer me to a question that deals with this specific error and possible solutions? Thanks – John Jul 19 '15 at 19:47
  • What issue are you having with `getInputStream`? I thought the issue was with a `null` catch parameter (which should not be possible). – Sotirios Delimanolis Jul 19 '15 at 19:48
  • I'm expecting the getInputStream() to execute normally so that it can carry on to the next lines of code (which I haven't displayed in this question). I'm just trying to figure out what's wrong with the way i'm using getInputStream(). Thanks – John Jul 19 '15 at 19:51
  • 1
    If you're getting `null` as a value for the exception parameter, you've got bigger problems. I'll re-open but try @ElliottFrisch's suggestion and use a different http client, where you can easily inspect the status code and response body. – Sotirios Delimanolis Jul 19 '15 at 20:00
  • @ElliottFrisch I just posted the stack trace – John Jul 19 '15 at 20:05
  • 1
    1. There is no such method as `InputStream.getInputStream()`. 2. Exceptions are thrown, not returned. 3. Thrown exceptions are never null, but their messages can be. 4. You should log the exception itself, not just its message. – user207421 Jul 19 '15 at 21:13
  • @EJP thhansk for point 1, i corrected the title. In any case, i was able to resolve the issue by temporarily using android.os.StrictMode in some places, and extending the ASync task in other places. – John Jul 20 '15 at 21:33

1 Answers1

2

i fixed the problem by putting this line of code before downloading the file:

            android.os.StrictMode.ThreadPolicy policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build();
            android.os.StrictMode.setThreadPolicy(policy);
John
  • 32,403
  • 80
  • 251
  • 422