0

I am trying to perform a simple get request using Apache HTTPClient however it seems as if all the code after the HTTPResponse response = client.execute(get); is being skipped. I am not able to access the contents of the response object,they are all null. However when I use debug mode and I explore the object I see all the data. This function is wrapped in an async task so I am wondering the task itself is not waiting on it to be executed or something I am not sure.

Something similar happened here: Android code after httpclient.execute(httpget) doesn't get run in try (using AsyncTask)

Here is the code.

 @Override
public T execute()
{
    utils = new GeneralUtils();
    if(getURL() == null)
    {
        throw new NullPointerException("No path specified");
    }
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(getURL());

    Log.e(TAG,"client created");
    if(getHeaders()!=null)
    {
        Log.e(TAG,"client created");
        for(Map.Entry<String,String> header:getHeaders().entrySet())
        {
            get.addHeader(header.getKey(),header.getValue());
        }
    }

    try
    {
        HttpResponse response = client.execute(get);
        Log.e(TAG,"executed");

        if(response==null)
            Log.v(TAG,"its null as heell");

        Log.v(TAG,response.getStatusLine().getReasonPhrase());
        Log.v(TAG,String.valueOf(response.getStatusLine().getStatusCode()));
        Log.e(TAG,getURL());

        Log.v(TAG,"everything else is dead");

        for(Header header:response.getAllHeaders())
        {
            Log.v(TAG,header.getName()+" "+header.getValue());
        }

        if(response.getStatusLine().getStatusCode() == 200)
        {
            if(response.getEntity().getContent()!=null)
            {

                try
                {
                    if(utils.isExternalStorageWritable())
                    {
                        String path = getContext().getFilesDir().getAbsolutePath()+"/"+getFileCategory()+"/" +getAlarmId()+getFileExtension();
                        media = new File(path);

                        /**
                         * if the directory has not being created this function does the creation.
                         */

                        media.mkdirs();

                        FileOutputStream fileOutputStream = new FileOutputStream(media);
                        IOUtils.copy(response.getEntity().getContent(),fileOutputStream);
                        fileOutputStream.close();

                        Log.e(TAG,media.getAbsolutePath());

                        return (T)media;

                    }
                    return null;


                }
                catch (ClientProtocolException e)
                {
                    Log.v(TAG,e.getMessage());
                }
                catch (IOException e)
                {
                    Log.v(TAG,e.getMessage());
                }
            }

        }
    }
    catch (IOException e)
    {
        Log.e(TAG, e.getCause().getMessage());
        e.printStackTrace();
    }

    return null;
}

The code is not throwing any exceptions so I am not sure about what's happening. All the code after the response object does not work. It just returns null, As in as soon as I try to obtain a value from response like so response.getStatusCode(), it seems as if the code goes dead and just returns null.

Community
  • 1
  • 1
Joel Dean
  • 2,444
  • 5
  • 32
  • 50

1 Answers1

0

Why don't you use a library that will handle all these restful connections?

I would recommend a couple:

https://github.com/darko1002001/android-rest-client (this is mine i have to mention it first :). I have built this library for the projects i build. For your case you would supply a parser which will give you an InputStream which you will just save as a file (as you do it now with IO utils). It handles the Asynchronous part of the whole thing and generally gives you a nice way to organize code.

http://square.github.io/retrofit/ - is another one that i have been playing around with. i think it is pretty well made and should be able to do whatever you want with it.

http://java.dzone.com/articles/android-%E2%80%93-volley-library - Volley is a project that came out straight from Google and it was demoed at the last Google IO conference. It handles all the async operations for you as well and enables you to do all these things. One thing that i am not really sure about is whether or not it will enable you to parse the responses in the background thread.

I would strongly suggest for you to use one of these as they might save you a lot of time. If you do want to continue with your code then i would suggest to first investigate if some of the "if" blocks you have are skipped, use the debugger or add log messages to see if it enters the blocks. Go step by step and see what goes wrong. I am doing something similar in my project, check out this file: https://github.com/darko1002001/android-rest-client/blob/master/android-rest-lib/src/main/java/com/dg/libs/rest/client/BaseRestClient.java

DArkO
  • 15,880
  • 12
  • 60
  • 88
  • I just tried to use square's Okhttp library and the same problem occurred. This is really weird. I am not sure what's causing this at all. – Joel Dean Oct 07 '13 at 20:26
  • Does it throw an exception? there must be an exception if it just skips everything below it. do you get anything in the logs? what is the URL you are trying to hit? – DArkO Oct 08 '13 at 06:33