Android HTTPClient导致应用崩溃

时间:2015-04-13 17:07:52

标签: android

我有这个Android代码:

package com.XXX

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

public class DataProvider {

    String baseUri;

    DataProvider() {
        baseUri = "http://www.XXX.de/XXX/";
    }

    public String requestUser(String userName) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet(baseUri + "get.php?userName=" + userName);
        ResponseHandler<String> handler = new BasicResponseHandler();

        String result = "";

        try {
            result = httpclient.execute(request, handler);
        } catch (ClientProtocolException e) {
            result = "ClientProtocolException was thrown";
        } catch (IOException e) {
            result = "IOException was thrown";
        }
        finally {
            httpclient.getConnectionManager().shutdown();
        }

        return result;
    }

}

和我的活动

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_reload_package) {
            String response = dataProvider.requestUser("BKDJSHDD-1912772-DIKDS-19172");
            browser.loadDataWithBaseURL("file:///android_asset/", response, "text/html", "UTF-8", "file:///android_asset/index.html");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

单击该菜单项会导致应用崩溃。对象browserdataProvider 100%正确声明。我在我的设备上测试应用程序,因为我的PC无法运行模拟器,因此没有控制台输出。

任何想法?:..

1 个答案:

答案 0 :(得分:2)

我的第一印象是HTTP请求的错误声明。在Android中,如果你想创建一个HttpRequest,你必须把它放在其他线程中,而不是在主线程中。

您的DataProvider.java在声明和空检查方面没问题,但不符合Android android.os.NetworkOnMainThreadException 的安全标准之一

使用AsyncTask运行HTTP请求,类似于您的Activity:

// The other part of your code ....

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_reload_package) {
        new DataProviderAsyncTask().execute();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

class DataProviderAsyncTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String response = dataProvider
                .requestUser("BKDJSHDD-1912772-DIKDS-19172");
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        browser.loadDataWithBaseURL("file:///android_asset/", result,
                "text/html", "UTF-8", "file:///android_asset/index.html");
    }

}

我希望你服务