Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.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
URLConnection在java中读取为null_Java_Url_Bufferedreader_Nul - Fatal编程技术网

URLConnection在java中读取为null

URLConnection在java中读取为null,java,url,bufferedreader,nul,Java,Url,Bufferedreader,Nul,我正在构建一个基本程序,用商店ID和产品ID查询目标公司的API,返回通道位置。但是,我认为我没有正确地使用URL构造函数(我在过去遇到过问题,仍然不能完全理解它们)。下面是我编写的代码,出于明显的原因对API键进行了编辑。我创建的URL在放入浏览器时是有效的,不会引发异常,但在最后打印页面内容时,它是空的。我错过了什么?非常感谢您的帮助 package productVerf; import java.net.*; import java.io.*; public class Verify

我正在构建一个基本程序,用商店ID和产品ID查询目标公司的API,返回通道位置。但是,我认为我没有正确地使用URL构造函数(我在过去遇到过问题,仍然不能完全理解它们)。下面是我编写的代码,出于明显的原因对API键进行了编辑。我创建的URL在放入浏览器时是有效的,不会引发异常,但在最后打印页面内容时,它是空的。我错过了什么?非常感谢您的帮助

package productVerf;

import java.net.*;
import java.io.*;

public class Verify {
    public static void main(String args[]) {
        // first input is store id second input is product id
        String productID = args[0];
        String storeID = args[1];
        String file = "/v2/products/storeLocations?productId=" + productID
                + "&storeId=" + storeID
                + "&storeId=694&key=REDACTED";
        URL locQuery;
        URLConnection lqConection = null;
        try {

            locQuery = new URL("http", "api.target.com", file);
            lqConection = locQuery.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader response;
        String responseString = "";
        try {
            response = new BufferedReader(new InputStreamReader(
                    lqConection.getInputStream()));
            while (response.readLine() != null) {
                responseString += response.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(responseString);

    }
}

也许你只是在读偶数行

你在读一行两遍?(在while语句…)中,看起来您读取了while条件测试中删除的第一行。如果您的响应只包含一行,则不会读取任何内容

使用以下命令:

String line;
while ((line=response.readLine()) != null) {
       responseString += line;
}

当您得到响应代码时,您会得到什么?