networkOnMainThread异常的Andr​​oid异常、networkOnMainThread、oid、Andr

由网友(南风又起,小镇微凉。)分享简介:我试图访​​问服务器,所以我可以得到一个JSON字符串。但显然在冰淇淋三明治,你可以在主线程不能做网络作业,但AsyncTask的类是混淆了我,而不是工作。这是我到目前为止有:I'm trying to access a server so I can receive a JSON String. But appar...

我试图访​​问服务器,所以我可以得到一个JSON字符串。但显然在冰淇淋三明治,你可以在主线程不能做网络作业,但AsyncTask的类是混淆了我,而不是工作。这是我到目前为止有:

I'm trying to access a server so I can receive a JSON String. But apparently in Ice Cream Sandwich you can't do network operations in the main thread, but the AsyncTask class is confusing me and not working. Here's what I have so far:

//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) {

        }

这不是喜欢我的 HTTPGET请求=新HTTPGET(URI) 它说,URI更改为URI,但它已经是! 我试图改变所有参数作废,但我的应用程序正义的力量关闭。

It's not liking my HttpGet request = new HttpGet(uri) It says to change uri to URI, but it already is! I've tried changing all parameters to Void, but my app just force closes.

任何人都知道如何做到这一点?

Anyone know how to do this?

推荐答案

试试这个例子:

    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) {

        }
    }
}

最后一张,您的服务器返回的PHP文件,它是正确的行为?

The last one, your server returns php file, is it right behaviour?

阅读全文

相关推荐

最新文章