Vktr
Vktr

Reputation: 89

Can't receive a value from HttpURLConnection.getResponseCode when using AsyncTask?

I'm trying to build an app that uses the OpenWeatherMap API.

I know that I can't send HTTP requests from the main thread. So I'm using AsyncTask instead.

Here's my MainActivity class from which I running the AsyncTask:

TextView txtView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtView = (TextView)findViewById(R.id.myTextID);

    MyTask task = new MyTask(MainActivity.this, txtView);
    task.execute();

}

And below is the MyTask class. This is where I send a GET request to a URL and try to get the response code.

public class MyTask extends AsyncTask<String, String, String>{

    Context context;
    TextView txtView;
    MyTask (Context context, TextView txtView)
    {
        this.context = context;
        this.txtView = txtView;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... voids) {

        HttpURLConnection connection = null;
        BufferedReader reader;
        String line;
        StringBuffer responseContent = new StringBuffer();
        String x = "No response code";



        try {
            URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=london&appid=thisismyaccesstoken");

            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");
            connection.setReadTimeout(10000);
            connection.setConnectTimeout(10000);

            x = Integer.toString(connection.getResponseCode());

        }
        catch(Exception e)
        {
           //exception handling done here
        }

        return x;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String s) {
        txtView.setText(s);
    }
}

I'm not trying to do any of the fancy loading bar stuff so I've kept the onProgressUpdate() method empty.

When I run the app, I get "No response code" on my screen, which is supposed to run only if the MyTask class failed to save the response code from the GET request into the string x.

This is the first time I'm playing around with AsyncTask. Is there something I'm doing incorrectly?

Upvotes: 0

Views: 107

Answers (1)

Vktr
Vktr

Reputation: 89

Fixed it. For some reason my app was crashing whenever I tried to print the error onto the console (in the catch block). So instead of printing to the console, I converted the exception into a string and saved the string in "x" like this:

catch(Exception e)
    {
       x = e.toString()
    }

This way the entire exception was displayed in the textview of my app. Apparently it was a SecurityException because my app didn't have permission to access the internet. So I just added the following lines in the AndroidManifest.xml file:

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

This solved the problem.

Upvotes: 0

Related Questions