如题,测试用的是知乎的一个API
https://zhuanlan.zhihu.com/api/columns/zhihuadmin/posts?limit=15&offset=0
但是就是返回不了东西,每次都是catch到了错误信息,错误信息就是这个网址。接口没错,用的是之前写好的,测试过能用,就是改了个网址而已。
接口是好的 具体在你访问接口拿数据那块出了问题
用的是get,网址能用网页正常打开,这样拿数据会有问题吗
@定不负相思懿: get 请求可以的
@ycyzharry: 那请问会是什么地方出错了呢
@定不负相思懿: 用我这个试试
public class HttpSendMessage {
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
@ycyzharry: 不行的,还是报错
@ycyzharry: 改了一下请求,已经可以了,谢谢。不知道您能否告诉我我之前的错误
看起来像是你进入了CORS的坑。
你不说你咋访问的,谁知道可能的错误在哪儿呢。
public static void sendHttpRequest(final String adress, final HttpCallbackListener linsener){
final Exception c=new Exception() ;
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection =null ;
try {
URL url=new URL(adress);
connection =(HttpURLConnection ) url.openConnection() ;
connection .setRequestMethod("GET");
connection .setConnectTimeout(10000);
connection .setReadTimeout(10000);
connection .setDoInput(true);
connection .setDoOutput(true);
InputStream in=connection.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(in)) ;
StringBuilder response=new StringBuilder();
String line;
while((line=reader .readLine()) !=null){
response .append(line) ;
}
if(linsener !=null){
linsener.onFinish(response .toString());
}
if(response ==null){
linsener .onError(c) ;
}
} catch (Exception e) {
linsener.onError(e);
} finally{
if(connection!=null ){
connection .disconnect();
}
}
}
}).start();
}
@定不负相思懿: 我看了下你代码 主要功能是好的 去掉一些内容后是可以访问的
public static void run(final String adress) {
HttpURLConnection connection = null;
try {
URL url = new URL(adress);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
System.out.println(response);
if (response == null) {
System.out.println("response为空");
}
} catch (Exception e) {
System.out.println("进入catch");
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
@ycyzharry: 换了一下代码,发现提示为java.io.FileNotFoundException: https://zhuanlan.zhihu.com/api/columns/zhihuadmin/posts?limit=15&offset=0,好像是IO流的问题?