0

I am really confused as to why I am having this problem. I am very new to android development so I am not really sure where to start when solving this problem. I have all the required permissions in the manifest file. <uses-permission android:name="android.permission.INTERNET" /> and <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />.

When a button is pressed myClickHandler is called and creates a URL with the two EditText's. After it does that it calls downloadWebPageTask which starts an ASyncTask. This AsyncTank takes the URL and then sends GET request. Here is my code.

public void myClickHandler(View view) {
    SetText url = new SetText();
    String stringUrl = url.createURL(artist_text.getText().toString(), release_text.getText().toString());
    new DownloadWebpageTask().execute(stringUrl);
}

public class DownloadWebpageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        //SendStuff send = new SendStuff("mrblahblahblacksheep");
        //return send.sendGet(urls[0]);
        String url = urls[0];
        String final_response = "FAILED!";

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        HttpResponse response;
        try {
            response = client.execute(request);

            final_response = response.toString();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return final_response;
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        result_text.setText(result);
    }
}

Every time I press the button in the Emulator the app crashes. None of the crash data is saved in the logcat so I am unsure as to wear to find out the reasons for the crash.

Josh Kruse
  • 31
  • 1
  • 3

1 Answers1

0

This is possibly the problem:

response = client.execute(request);
final_response = response.toString();

If response is null, then response.toString() will throw NPE and your app will crash.

Make error handling graceful.

Try returning null result from catch blocks when you encounter exceptions. Also, for NPEs make if-else blocks and return null explicitly.

In onPostExecute(...) check if result is null, then display some default text or else display expected result.

Mithun
  • 2,075
  • 3
  • 19
  • 26
  • Doesn't seem to be the problem. The app crashes before `response.toString()` is set. – Josh Kruse May 17 '15 at 00:34
  • I tried `new DownloadWebpageTask().execute("http://www.thomas-bayer.com/sqlrest/CUSTOMER/3/");` it works for me perfectly. Unless we know what URL you are using its very hard to pinpoint the issue. Could you try http://stackoverflow.com/questions/6854127/filter-logcat-to-get-only-the-messages-from-my-application-in-android to get logs for your app. Also, what device and OS you are using. – Mithun May 17 '15 at 10:06