简体   繁体   English

尝试使用ASyncTask发出GET请求时,如何防止我的应用程序崩溃?

[英]How can I stop my app from crashing when trying to make a GET request with an ASyncTask?

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. 我对android开发非常陌生,因此我不确定在解决此问题时从哪里开始。 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" /> . <uses-permission android:name="android.permission.INTERNET" /><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. 当按下按钮时,将myClickHandler并使用两个EditText创建一个URL。 After it does that it calls downloadWebPageTask which starts an ASyncTask. 完成后,它将调用downloadWebPageTask ,这将启动ASyncTask。 This AsyncTank takes the URL and then sends GET request. 此AsyncTank接收URL,然后发送GET请求。 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. 崩溃数据没有保存在logcat中,因此我不确定是否要找出崩溃的原因。

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. 如果response为null,则response.toString()将抛出NPE并且您的应用程序将崩溃。

Make error handling graceful. 使错误处理更加顺畅。

Try returning null result from catch blocks when you encounter exceptions. 遇到异常时,请尝试从catch块返回null结果。 Also, for NPEs make if-else blocks and return null explicitly. 另外,对于NPE,使用if-else块并显式返回null

In onPostExecute(...) check if result is null, then display some default text or else display expected result. onPostExecute(...)检查result是否为null,然后显示一些默认文本,否则显示预期结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM