在后台打开网站时,应用程序崩溃

时间:2016-01-04 17:01:49

标签: android android-asynctask

我正在尝试在我的Android应用程序中打开一个网址“http://../member.php?q=8”,但我无法做到。

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

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                responseString = out.toString();
                out.close();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

以上是我的AsyncTask用法,每当我执行:new OpenUrlBackground().execute("the above link")时,我的应用程序崩溃,上面的url不会在后台打开。 请帮忙

0 个答案:

没有答案