通过URL使用来自网站的数据填充Textview

时间:2015-07-29 02:45:49

标签: javascript java android android-activity android-asynctask

我尝试从网址读取一行文字并在Textview中显示结果。

ISSUE / ERROR:

目前没有错误,我执行DownloadWebPage异步任务后没有填充textview。

INVESTIGATIONS

我相信我可能没有正确地调用它,因为我无法在DownloadWebPage中找到调试调用的任何结果。此外,我的URL有一行代码,带有两个字符串,如果这有任何区别。

UPDATE:

在DownloadWebPage中,我的日志命令没有显示在日志文件中 - 很奇怪

代码:ON CREATE:

public void onCreate(Bundle savedInstanceState) 

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inbox_list);
       textView = (TextView) findViewById(R.id.textView6);

      // Hashmap for ListView
       inboxList = new ArrayList<HashMap<String, String>>();

       //ASYNC TASK I CREATED
       new DownloadWebPageTask().execute();

      // Another Async Task in my code
       new LoadInbox().execute();


  }

CODE:用于从URL解析数据的AsyncTask&amp;将数据设置为Textview

 private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                System.out.println(response);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            textView.setText(Html.fromHtml(result));
        }
    }

    public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.url.com/blah.php" });

    }

0 个答案:

没有答案