首页 > Android 无法获取httpClient的数据?

Android 无法获取httpClient的数据?

我在主函数里写了一个方法来获取http请求的数据。。一直无法运行。

下面这段程序我在调试的时候连thread方法都没法进去。。

void   getNametext(final String string) {


        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(string);
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        System.out.print("-------------"+httpResponse.getStatusLine().getStatusCode());
                        HttpEntity entity = httpResponse.getEntity();
                        String response = EntityUtils.toString(entity, "utf-8");

                        Gson gson = new Gson();
                        ArrayList<test> arrayList = gson.fromJson(response, new TypeToken<ArrayList<test>>() {
                        }.getType());

                        for (test one :
                                arrayList) {
                            Log.d("out",one.toString());
                        }


                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });


        }

这个方法在主函数里面被调用、


  getNametext("http://localhost/get_data.json");

链接里面的数据也没有错误,是写在本地的一个JSON数据

测试时候用的是真机。。实在找不出是哪里错误。。求指教~~谢谢


你要不试试网上的数据?本地数据很暧昧的....
还有这个没.start()能启动?


把localhost换成你的ip试试?


你线程都没有启动,怎么操作?

new Thread().start();

thread 要运行需要加上start
另外,在这里面打上log看一下是不是exception了

catch (IOException e) {
  e.printStackTrace();
}

有捕捉到异常吗? getNametext()有放在onCreate()方法中吗? 真机的浏览器能访问到这个数据吗?

【热门文章】
【热门文章】