Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取HttpURLConnection_Java_Android_Bufferedreader_Httpurlconnection_Inputstreamreader - Fatal编程技术网

Java 读取HttpURLConnection

Java 读取HttpURLConnection,java,android,bufferedreader,httpurlconnection,inputstreamreader,Java,Android,Bufferedreader,Httpurlconnection,Inputstreamreader,我一直在想如何读取HttpURLConnection。根据这个例子:,下面的代码应该可以工作。然而,readStream从不启动,我也不记录任何行 我确实知道InputStream是通过缓冲区和all传递的,但对我来说,readStream方法的逻辑崩溃了,然后主要是空字符串“line”和while语句。那里到底发生了什么/应该发生什么,我怎样才能解决它?另外,为什么我必须在Try语句中创建url?它返回一个未处理的异常;java.net.MalformedURLException 提前谢谢 s

我一直在想如何读取HttpURLConnection。根据这个例子:,下面的代码应该可以工作。然而,readStream从不启动,我也不记录任何行

我确实知道InputStream是通过缓冲区和all传递的,但对我来说,readStream方法的逻辑崩溃了,然后主要是空字符串“line”和while语句。那里到底发生了什么/应该发生什么,我怎样才能解决它?另外,为什么我必须在Try语句中创建url?它返回一个未处理的异常;java.net.MalformedURLException

提前谢谢

static String SendURL(){
    try {
        URL url = new URL("http://www.google.com/");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
          readStream (con.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ("Done");

}

static void readStream(InputStream in) {

    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) {
            Log.i("Tag", line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

您似乎没有连接您的HTTPUrlClient try con.connect()

我在问题中发布的代码有很多问题。以下是一个工作示例:

public class GooglePlaces extends AsyncTask {

public InputStream inputStream;


    public GooglePlaces(Context context) {

        String url = "https://www.google.com";


        try {
            HttpRequest httpRequest = requestFactory.buildGetRequest(new GenericUrl(url));
            HttpResponse httpResponse = httpRequest.execute();
            inputStream = httpResponse.getContent();

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


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();

        try {
            for (String line = null; (line = bufferedReader.readLine()) != null;) {
                builder.append(line).append("\n");
                Log.i("GooglePlacesTag", line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

看一看。在那里,您可以找到
HttpURLConnection
的抽象库和备选方案列表
HttpURLConnection
太麻烦了,无法使用,只需搜索它,您就会看到。它看起来非常有希望,我将在今晚稍后试一试,并将向您汇报。谢谢