- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在发出 http 请求并从 API 获取 json 数据。我尝试打印收到的内容,但它没有打印任何内容,也没有错误,我似乎找不到原因
这是一个 API 项目
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main
{
private static HttpURLConnection connection;
public static void main(String[] args)
{
BufferedReader reader;
String line;
StringBuffer responseContent = new StringBuffer();
// Method 1 java.net.HttpURLConnection
try
{
URL url = new URL("https://jsonplaceholder.typicode.com/albums");
connection = (HttpURLConnection) url.openConnection();
// Request Setup
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
System.out.println(status);
if(status > 299)
{
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
while((line = reader.readLine())!= null)
{
responseContent.append(line);
}
reader.close();
}
else
{
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null)
{
responseContent.append(line);
}
reader.close();
System.out.println("In else.");
}
System.out.println(responseContent.toString());
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
connection.disconnect();
}
}
}
我正在尝试获取整个 https://jsonplaceholder.typicode.com/albums当您在浏览器中搜索此内容时通常会看到该内容。但没有打印任何内容,也没有错误。我对 API 和 http 很陌生,所以如果复杂请详细解释
最佳答案
我按原样运行了您的代码,这就是它打印的内容:
200
In else.
[ { "userId": 1, "id": 1, "title": "quidem molestiae enim" }, { "userId": 1, "id": 2, "title": "sunt qui excepturi placeat culpa" }, { "userId": 1, "id": 3, "title": "omnis laborum odio" }, { "userId": 1, "id": 4, "title": "non esse culpa molestiae omnis sed optio" }, { "userId": 1, "id": 5, "title": "eaque aut omnis a" }, { "userId": 1, "id": 6, "title": "natus impedit quibusdam illo est" }, { "userId": 1, "id": 7, "title": "quibusdam autem aliquid et et quia" }, { "userId": 1, "id": 8, "title": "qui fuga est a eum" }, { "userId": 1, "id": 9, "title": "saepe unde necessitatibus rem" }, { "userId": 1, "id": 10, "title": "distinctio laborum qui" }, { "userId": 2, "id": 11, "title": "quam nostrum impedit mollitia quod et dolor" }, { "userId": 2, "id": 12, "title": "consequatur autem doloribus natus consectetur" }, { "userId": 2, "id": 13, "title": "ab rerum non rerum consequatur ut ea unde" }, { "userId": 2, "id": 14, "title": "ducimus molestias eos animi atque nihil" }, { "userId": 2, "id": 15, "title": "ut pariatur rerum ipsum natus repellendus praesentium" }, { "userId": 2, "id": 16, "title": "voluptatem aut maxime inventore autem magnam atque repellat" }, { "userId": 2, "id": 17, "title": "aut minima voluptatem ut velit" }, { "userId": 2, "id": 18, "title": "nesciunt quia et doloremque" }, { "userId": 2, "id": 19, "title": "velit pariatur quaerat similique libero omnis quia" }, { "userId": 2, "id": 20, "title": "voluptas rerum iure ut enim" }, { "userId": 3, "id": 21, "title": "repudiandae voluptatem optio est consequatur rem in temporibus et" }, { "userId": 3, "id": 22, "title": "et rem non provident vel ut" }, { "userId": 3, "id": 23, "title": "incidunt quisquam hic adipisci sequi" }, { "userId": 3, "id": 24, "title": "dolores ut et facere placeat" }, { "userId": 3, "id": 25, "title": "vero maxime id possimus sunt neque et consequatur" }, { "userId": 3, "id": 26, "title": "quibusdam saepe ipsa vel harum" }, { "userId": 3, "id": 27, "title": "id non nostrum expedita" }, { "userId": 3, "id": 28, "title": "omnis neque exercitationem sed dolor atque maxime aut cum" }, { "userId": 3, "id": 29, "title": "inventore ut quasi magnam itaque est fugit" }, { "userId": 3, "id": 30, "title": "tempora assumenda et similique odit distinctio error" }, { "userId": 4, "id": 31, "title": "adipisci laborum fuga laboriosam" }, { "userId": 4, "id": 32, "title": "reiciendis dolores a ut qui debitis non quo labore" }, { "userId": 4, "id": 33, "title": "iste eos nostrum" }, { "userId": 4, "id": 34, "title": "cumque voluptatibus rerum architecto blanditiis" }, { "userId": 4, "id": 35, "title": "et impedit nisi quae magni necessitatibus sed aut pariatur" }, { "userId": 4, "id": 36, "title": "nihil cupiditate voluptate neque" }, { "userId": 4, "id": 37, "title": "est placeat dicta ut nisi rerum iste" }, { "userId": 4, "id": 38, "title": "unde a sequi id" }, { "userId": 4, "id": 39, "title": "ratione porro illum labore eum aperiam sed" }, { "userId": 4, "id": 40, "title": "voluptas neque et sint aut quo odit" }, { "userId": 5, "id": 41, "title": "ea voluptates maiores eos accusantium officiis tempore mollitia consequatur" }, { "userId": 5, "id": 42, "title": "tenetur explicabo ea" }, { "userId": 5, "id": 43, "title": "aperiam doloremque nihil" }, { "userId": 5, "id": 44, "title": "sapiente cum numquam officia consequatur vel natus quos suscipit" }, { "userId": 5, "id": 45, "title": "tenetur quos ea unde est enim corrupti qui" }, { "userId": 5, "id": 46, "title": "molestiae voluptate non" }, { "userId": 5, "id": 47, "title": "temporibus molestiae aut" }, { "userId": 5, "id": 48, "title": "modi consequatur culpa aut quam soluta alias perspiciatis laudantium" }, { "userId": 5, "id": 49, "title": "ut aut vero repudiandae voluptas ullam voluptas at consequatur" }, { "userId": 5, "id": 50, "title": "sed qui sed quas sit ducimus dolor" }, { "userId": 6, "id": 51, "title": "odit laboriosam sint quia cupiditate animi quis" }, { "userId": 6, "id": 52, "title": "necessitatibus quas et sunt at voluptatem" }, { "userId": 6, "id": 53, "title": "est vel sequi voluptatem nemo quam molestiae modi enim" }, { "userId": 6, "id": 54, "title": "aut non illo amet perferendis" }, { "userId": 6, "id": 55, "title": "qui culpa itaque omnis in nesciunt architecto error" }, { "userId": 6, "id": 56, "title": "omnis qui maiores tempora officiis omnis rerum sed repellat" }, { "userId": 6, "id": 57, "title": "libero excepturi voluptatem est architecto quae voluptatum officia tempora" }, { "userId": 6, "id": 58, "title": "nulla illo consequatur aspernatur veritatis aut error delectus et" }, { "userId": 6, "id": 59, "title": "eligendi similique provident nihil" }, { "userId": 6, "id": 60, "title": "omnis mollitia sunt aliquid eum consequatur fugit minus laudantium" }, { "userId": 7, "id": 61, "title": "delectus iusto et" }, { "userId": 7, "id": 62, "title": "eos ea non recusandae iste ut quasi" }, { "userId": 7, "id": 63, "title": "velit est quam" }, { "userId": 7, "id": 64, "title": "autem voluptatem amet iure quae" }, { "userId": 7, "id": 65, "title": "voluptates delectus iure iste qui" }, { "userId": 7, "id": 66, "title": "velit sed quia dolor dolores delectus" }, { "userId": 7, "id": 67, "title": "ad voluptas nostrum et nihil" }, { "userId": 7, "id": 68, "title": "qui quasi nihil aut voluptatum sit dolore minima" }, { "userId": 7, "id": 69, "title": "qui aut est" }, { "userId": 7, "id": 70, "title": "et deleniti unde" }, { "userId": 8, "id": 71, "title": "et vel corporis" }, { "userId": 8, "id": 72, "title": "unde exercitationem ut" }, { "userId": 8, "id": 73, "title": "quos omnis officia" }, { "userId": 8, "id": 74, "title": "quia est eius vitae dolor" }, { "userId": 8, "id": 75, "title": "aut quia expedita non" }, { "userId": 8, "id": 76, "title": "dolorem magnam facere itaque ut reprehenderit tenetur corrupti" }, { "userId": 8, "id": 77, "title": "cupiditate sapiente maiores iusto ducimus cum excepturi veritatis quia" }, { "userId": 8, "id": 78, "title": "est minima eius possimus ea ratione velit et" }, { "userId": 8, "id": 79, "title": "ipsa quae voluptas natus ut suscipit soluta quia quidem" }, { "userId": 8, "id": 80, "title": "id nihil reprehenderit" }, { "userId": 9, "id": 81, "title": "quibusdam sapiente et" }, { "userId": 9, "id": 82, "title": "recusandae consequatur vel amet unde" }, { "userId": 9, "id": 83, "title": "aperiam odio fugiat" }, { "userId": 9, "id": 84, "title": "est et at eos expedita" }, { "userId": 9, "id": 85, "title": "qui voluptatem consequatur aut ab quis temporibus praesentium" }, { "userId": 9, "id": 86, "title": "eligendi mollitia alias aspernatur vel ut iusto" }, { "userId": 9, "id": 87, "title": "aut aut architecto" }, { "userId": 9, "id": 88, "title": "quas perspiciatis optio" }, { "userId": 9, "id": 89, "title": "sit optio id voluptatem est eum et" }, { "userId": 9, "id": 90, "title": "est vel dignissimos" }, { "userId": 10, "id": 91, "title": "repellendus praesentium debitis officiis" }, { "userId": 10, "id": 92, "title": "incidunt et et eligendi assumenda soluta quia recusandae" }, { "userId": 10, "id": 93, "title": "nisi qui dolores perspiciatis" }, { "userId": 10, "id": 94, "title": "quisquam a dolores et earum vitae" }, { "userId": 10, "id": 95, "title": "consectetur vel rerum qui aperiam modi eos aspernatur ipsa" }, { "userId": 10, "id": 96, "title": "unde et ut molestiae est molestias voluptatem sint" }, { "userId": 10, "id": 97, "title": "est quod aut" }, { "userId": 10, "id": 98, "title": "omnis quia possimus nesciunt deleniti assumenda sed autem" }, { "userId": 10, "id": 99, "title": "consectetur ut id impedit dolores sit ad ex aut" }, { "userId": 10, "id": 100, "title": "enim repellat iste" }]
所以,您的代码正在运行,它正在生成 GET 请求,获取数据,并将其打印出来。也许您的控制台有问题和/或您正在读取控制台输出。
关于java - 我正在尝试打印 StringBuffer.toString() 但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56860241/
为什么下一行没有给出编译时错误? StringBuffer sb = new StringBuffer(new StringBuffer()); 我在网上查了很多也找不到原因。不仅如此,它还允许 St
我在 http get 请求中使用了下面的代码,但是我从返回中得到的是 null。我不知道为什么。 public static String getResponseFromGetUrl(Strin
错误是: Unreachable statement in line StringBuffer buffer = new StringBuffer() 这是我的代码: public void
当 StringBuffer 超出容量时,是否会创建一个新的 StringBuffer 对象,还是仍然是旧的? class Test{ public static void main(String[]
1.我对这两者感到困惑,如果有的话,它们有不同的功能吗? StringBuffer(CharSequence chars) 和 StringBuffer(String str) 2。 String 和
阅读本文后 - What does 'synchronized' mean?我仍然无法理解为什么 StringBuffer 在线程安全环境中会比 StringBuilder 慢。 StringBuff
考虑以下代码 final class immudemo { private static final StringBuffer bf = new StringBuffer("Yaxita"
Groovy支持用于创建StringBuilder / StringBuffer的文字语法,而不是通常的语法 def sb = new StringBuilder() 但是,我似乎无法记住(或在Goo
这是我尝试使用数组构建 stringBuffer。我该如何解决这个问题? import java.util.ArrayList; public class StringBufferProj { pub
我对官方 Javadoc 的说法感到困惑 public StringBuffer replace(int start, int end, String str) Replaces the charac
与将所有数组项附加到 StringBuffer 然后打印相比,为什么在循环中打印数组项需要更多时间? 在此,我在循环中打印了数组项。 public static void main (String[]
我目前正在使用很多这样的内容来重构应用程序: StringBuffer buff1 = new StringBuffer(""); buff1.append("some value A"); buff
我有一个简单的函数,用于连接到服务器并将响应作为字符串返回。当返回的数据量较小但响应较大时,它可以正常工作。它不会完整存储服务器返回的响应字符串,并以...结尾。令人惊讶的是,system.out.p
我对编程还很陌生,需要一些帮助。我正在学习如何使用 StringBuffer 类,并且编写了一个简单的代码。但是,当我尝试运行该程序时,我不断收到错误消息“找不到符号”。任何建议都会很棒!谢谢! pu
在ArrayList中,添加操作是摊销操作。因此,在阅读 StringBuffer 时,我想到了为什么 StringBuffer 不进行摊销。假设我对字符串缓冲区对象使用追加操作,那么它应该能够在其底
我正在尝试在字符串中切换 $ 及其右侧的字符。我不允许使用 char[],所以我决定使用 StringBuffer。但是,当我尝试使用 H$E 之类的内容运行代码时,它会输出 HE$H$E 我不知道额
这个问题已经有答案了: Difference between StringBuilder and StringBuffer (33 个回答) 已关闭 9 年前。 很多人都提到过Java中StringB
我正在尝试从InputStream 中读取一些数据,然后将它们放入StringBuffer 中进行打印。 我把这段代码放在main方法中。 我的问题是,只有在调试代码时才打印 StringBuffer
我正在运行此代码: public class testttt { public static void main(String[] args){ ArrayList listOne = new
我有以下代码: public class MyLogger { private StringBuilder logger = new StringBuilder(); public voi
我是一名优秀的程序员,十分优秀!