从URL ANDROID获取内容内容、URL、ANDROID

2023-09-04 03:52:31 作者:往事隔山水

我有一个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>);