1

an I am trying to load an image from my Server with Android and REST. But when I try to open the stream it crashes with this exception:

myapp: W/System.err? java.io.IOException: No authentication challenges found
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
myapp: W/System.err? at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
myapp: W/System.err? at java.net.URL.openStream(URL.java:462)
myapp: W/System.err? at de.facentis.GenerstoAndroidTester.Rest.asynctasks.TheOneAndOnlyAsync.getStream(TheOneAndOnlyAsync.java:199)
myapp: W/System.err? at de.facentis.GenerstoAndroidTester.Rest.asynctasks.TheOneAndOnlyAsync.doInBackground(TheOneAndOnlyAsync.java:74)
myapp: W/System.err? at de.facentis.GenerstoAndroidTester.Rest.asynctasks.TheOneAndOnlyAsync.doInBackground(TheOneAndOnlyAsync.java:22)
myapp: W/System.err? at android.os.AsyncTask$2.call(AsyncTask.java:287)
myapp: W/System.err? at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
myapp: W/System.err? at java.util.concurrent.FutureTask.run(FutureTask.java:137)
myapp: W/System.err? at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
myapp: W/System.err? at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
myapp: W/System.err? at java.lang.Thread.run(Thread.java:856)

I found in other threads, here on stackoverflow, it is maybe a 401 from server. I check it and my server sends me a 200 statuscode. The method from server works correctly on other clients.

Here is the method I use:

try {
    InputStream input = null;
    OutputStream output = null;

    try {
        URL url = new URL(methodUri);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

        connection.setRequestProperty("Authorization", basicHeader);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage());
        connection.setConnectTimeout(5000);
        connection.connect();

        input = new BufferedInputStream(url.openStream());      // <--- Crash
        output = new FileOutputStream(context.getFileStreamPath(filename));

        byte data[] = new byte[1024];
        int count;
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
        }

        return;
    } catch (java.net.SocketTimeoutException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }finally{
        try {
            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}catch (Exception ex) {
    ex.printStackTrace();
}
return;

Someone na idea?

best regards

BHuelse
  • 2,889
  • 3
  • 30
  • 41
  • see answer http://stackoverflow.com/questions/11810447/httpurlconnection-worked-fine-in-android-2-x-but-not-in-4-1-no-authentication-c – Pihhan Oct 23 '15 at 11:54
  • I the cases from the other post, the users getting 401. I am getting 200 as answer. I have in the same project a method for GET and POST both are working correctly. Just the stream is crashing. – BHuelse Oct 23 '15 at 15:28

1 Answers1

1

I found a solution, but dont know why it works. If someone can explain why it works, you are welcome.

I switch from:

input = new BufferedInputStream(url.openStream());

to:

input = connection.getInputStream();

best regards

BHuelse
  • 2,889
  • 3
  • 30
  • 41