简体   繁体   English

为什么bufferReader在readLine()上返回null

[英]why bufferreader returns null on readLine()

I'm creating a BufferReader from InputStreamReader which loads response from webservice like this: 我正在从InputStreamReader创建一个BufferReader ,它从Web服务加载响应,如下所示:

HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
try {
    InputStream in = c.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    // returns null
    String line = reader.readLine();

    // correctly instantiates Article object
    Article article = new Gson().fromJson(reader, Article.class); 

However, for some reason, reader.readLine(); 但是,由于某种原因, reader.readLine(); returns null , although the data successfully came from the server - I know that because Article object is correctly instantiated from the returned JSON. 返回null ,尽管数据成功地来自服务器-我知道这是因为从返回的JSON正确实例化了Article对象。 What could be the reason for returning null ? 返回null的原因可能是什么?

Hm, that's odd... It worked perfectally fine for me when I did this: 嗯,这很奇怪...当我这样做时,它对我来说非常好:

File file = new File("plugin.yml");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;

while((line = reader.readLine()) != null) {
    System.out.println(line);
}

reader.close();

I know, you have stated that you have tested everything with the System.out.println(stuff), but I'll still ask the most basic questions and maybe the problem is hidden somewhere in there: Does it even enter the method? 我知道,您已经说过您已经使用System.out.println(stuff)测试了所有内容,但是我仍然会问一些最基本的问题,也许该问题隐藏在其中:它甚至可以输入方法吗? Is the file correctly spelled (also from where you add log to the log)? 该文件的拼写是否正确(也是从将日志添加到日志的位置)? Does it work, if you use the Scanner to read the file? 如果您使用扫描仪读取文件,是否可以使用?

Scanner scan = new Scanner(file);
String line;

while(scan.hasNextLine()) {
    line = scan.nextLine();
    //...
}

scan.close();

These are probably all silly questions, but it might help. 这些可能都是愚蠢的问题,但这可能会有所帮助。 As for now, I can't see any problems in your code. 到目前为止,在您的代码中看不到任何问题。

I would assume you missed to set the connection output to true. 我认为您错过了将连接输出设置为true的机会。 Furthermore I'd do it something like this: 此外,我会做这样的事情:

try {
  URL url = new URL("http://im-an-url.com");
  HttpURLConnection c = (HttpURLConnection) url.openConnection();
  c.setDoOutput(true);
  OutputStreamWriter wr = new OutputStreamWriter(c.getOutputStream());
  //do whatever you like here with the StreamWriter
  wr.close()
}
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
try {
c.connect();
if (c.getResponseCode == 200) {


InputStream in = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

// returns null
String line = reader.readLine();

// correctly instantiates Article object
Article article = new Gson().fromJson(reader, Article.class); 
 }

make sure the net stream has end tag \\r\\n if you use readLine method, or you can use BufferOutputStream 如果使用readLine方法,请确保净流具有结束标记\\ r \\ n,或者可以使用BufferOutputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM