我正在尝试使用 api.stackexchange 上的 Apache Api(httpcomponents)在 Java 中执行 http 请求。但是这个请求返回文本而不是 html。
这是我的代码:
public class HttpRequestBrute {
public static void main(String[] args) throws Exception {
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("api.stackexchange.com").setPath("/2.0/search")
.setParameter("site", "stackoverflow")
.setParameter("intitle" ,"workaround")
.setParameter("tagged","javascript");
URI uri = builder.build();
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget1 = new HttpGet(uri);
System.out.println("executing request " + httpget1.getURI());
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget1, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
httpclient.getConnectionManager().shutdown();
}
}
}
第 1 行第 19 列的返回 json 值似乎有错误。
线程“main”com.google.gson.JsonSyntaxException 中的异常:java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 19 列为 STRING