networkOnMainThread异常Android

networkOnMainThread异常Android,android,networking,android-asynctask,android-4.0-ice-cream-sandwich,http-get,Android,Networking,Android Asynctask,Android 4.0 Ice Cream Sandwich,Http Get,我正在尝试访问服务器,以便接收JSON字符串。但是很明显,在冰激凌三明治中,您不能在主线程中执行网络操作,但是AsyncTask类让我感到困惑,无法工作。以下是我目前掌握的情况: //up in main code Void blah = null; URI uri = "kubie.dyndns-home.com/R2Bar2/ingredients.php"; new DownloadFilesTask().execute(uri , blah, blah); private class

我正在尝试访问服务器,以便接收JSON字符串。但是很明显,在冰激凌三明治中,您不能在主线程中执行网络操作,但是AsyncTask类让我感到困惑,无法工作。以下是我目前掌握的情况:

//up in main code
Void blah = null;
URI uri = "kubie.dyndns-home.com/R2Bar2/ingredients.php";
new DownloadFilesTask().execute(uri , blah, blah);


private class DownloadFilesTask extends AsyncTask<URI, Void, Void> {
        protected Void doInBackground(URI... uri) {
            HttpClient client = new DefaultHttpClient();
            String json = "";
            int duration = Toast.LENGTH_SHORT;
            try {
                HttpResponse response = null;
                BufferedReader rd = null;
                String line = "";
                HttpGet request = new HttpGet(uri);
            } catch (URISyntaxException e1) {

            }
return null;
        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(Long result) {

        }
//在主代码中向上
Void blah=null;
URI URI=“kubie.dyndns home.com/R2Bar2/components.php”;
新建DownloadFilesTask().execute(uri,等等);
私有类DownloadFilesTask扩展了AsyncTask{
受保护的Void doInBackground(URI…URI){
HttpClient=new DefaultHttpClient();
字符串json=“”;
int duration=Toast.LENGTH\u SHORT;
试一试{
HttpResponse响应=null;
BufferedReader rd=null;
字符串行=”;
HttpGet请求=新的HttpGet(uri);
}捕获(URISyntaxException e1){
}
返回null;
}
受保护的void onProgressUpdate(整数…进度){
}
受保护的void onPostExecute(长结果){
}
它不喜欢我的
HttpGet请求=newhttpget(uri)
它说要将uri更改为uri,但它已经是了! 我尝试将所有参数更改为Void,但我的应用程序只是强制关闭

有人知道怎么做吗

但很明显,在冰激凌三明治中,你不能在主线程中进行网络操作

您可以,但它会在LogCat中给您一个警告。您不应该在主应用程序线程上执行网络操作,因为它会冻结您的UI,可能会给您一个“应用程序无响应”(ANR)对话框

但是AsyncTask类让我感到困惑,无法工作

在当前代码中,您实际上没有执行
HttpGet
请求

它不喜欢我的HttpGet request=newhttpget(uri),它说要将uri更改为uri,但它已经是了

不,不是

doInBackground()
声明中
URI
之后的
..
表示此方法接受数量可变的参数。如果您只调用
execute(),则
URI
参数实际上是一个
URI[]
使用一个
URI
,您可以通过
URI[0]
获得
URI
,而不是
URI

尝试以下示例:

    public class Test_anroidActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String uri = new String("http://kubie.dyndns-home.com/R2Bar2/ingredients.php");
        new DownloadFilesTask().execute(uri , null, null);
    }
    private class DownloadFilesTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            HttpClient client = new DefaultHttpClient();
            String json = "";
            try {
                String line = "";
                HttpGet request = new HttpGet(urls[0]);
                HttpResponse response = client.execute(request);
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null) {
                    json += line + System.getProperty("line.separator");
                }
            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
            return json;
        }

        protected void onProgressUpdate(Void... progress) {

        }

        protected void onPostExecute(String result) {

        }
    }
}
公共类测试活动扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字符串uri=新字符串(“http://kubie.dyndns-home.com/R2Bar2/ingredients.php");
新建DownloadFileTask().execute(uri,null,null);
}
私有类DownloadFilesTask扩展了AsyncTask{
受保护的字符串doInBackground(字符串…URL){
HttpClient=new DefaultHttpClient();
字符串json=“”;
试一试{
字符串行=”;
HttpGet请求=新的HttpGet(URL[0]);
HttpResponse response=client.execute(请求);
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
而((line=rd.readLine())!=null){
json+=line+System.getProperty(“line.separator”);
}
}捕获(IllegalArgumentException e1){
e1.printStackTrace();
}捕获(IOE2异常){
e2.printStackTrace();
}
返回json;
}
受保护的void onProgressUpdate(void…progress){
}
受保护的void onPostExecute(字符串结果){
}
}
}

最后一个,您的服务器返回php文件,这是正确的行为吗?

Oops,我忘记添加:'try{response=client.execute(request);}catch(ClientProtocolException e){'但它仍然强制关闭。我甚至需要该类的任何参数吗?我可以在'doInBackground()中创建URI吗'方法?是的!非常感谢!我认为Toast通知不起作用。我必须返回字符串,然后解码JSON。再次感谢!