从URL ANDROID获取内容 [英] ANDROID get content from url

查看:140
本文介绍了从URL ANDROID获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android的一个问题:

I have a problem with Android:

我必须要能够从产生这些URL中读取内容,然后把它们放在一个数组分割读取的字符串。我使用了不同的方法,但我无法读取该字符串。我该怎么办?

I have to be able to read content from a URL that generates them and then put them in an array Splitting the string read. I have used different methods but I can not read that string. How can I do?

这是我现在使用的函数(继续出现字符串NN VA):

This is the function that I now use (continue to appear the string "nn va"):

private void vaia () { // funzione vaia *******************************************************

        try {
            HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
            HttpGet httpget = new HttpGet(URL); // Set the action you want to do
            HttpResponse response = httpclient.execute(httpget); // Executeit
            HttpEntity entity = response.getEntity(); 
            InputStream is = entity.getContent(); // Create an InputStream with the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) // Read line by line
                sb.append(line + "\n");

            String resString = sb.toString(); // Result is here
            Log.i("string letta", resString);

            is.close(); // Close the stream
        } 
        catch (Exception e) {
            Log.i("Risultato eccezione","nn va");
              //e.printStackTrace();
        }


    }

网址这一点: http://www.innovacem.com/public/糖霜/ leggiFoto.php (应该写VUOTO)

the URL to this point: http://www.innovacem.com/public/glace/leggiFoto.php (should write "vuoto")

感谢:)

推荐答案

使用异步任务:

private class ReadTask extends AsyncTask<String, Integer, String> 
{

    @Override
    protected String doInBackground(String... params) 
    {

        getResponseFromUrl(params[0]);
        return "success";
    }   
}

要调用它使用:

new ReadTask().execute(<your url>);

这篇关于从URL ANDROID获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆