首页 新闻 会员 周边

Android get不到数据

0
悬赏园豆:5 [已解决问题] 解决于 2017-05-25 15:29

  如题,测试用的是知乎的一个API

https://zhuanlan.zhihu.com/api/columns/zhihuadmin/posts?limit=15&offset=0

但是就是返回不了东西,每次都是catch到了错误信息,错误信息就是这个网址。接口没错,用的是之前写好的,测试过能用,就是改了个网址而已。
定不负相思懿的主页 定不负相思懿 | 初学一级 | 园豆:132
提问于:2017-05-25 10:03
< >
分享
最佳答案
0

接口是好的 具体在你访问接口拿数据那块出了问题

收获园豆:5
ycyzharry | 高人七级 |园豆:25653 | 2017-05-25 10:18

用的是get,网址能用网页正常打开,这样拿数据会有问题吗

定不负相思懿 | 园豆:132 (初学一级) | 2017-05-25 10:24

@定不负相思懿: get 请求可以的

ycyzharry | 园豆:25653 (高人七级) | 2017-05-25 10:25

@ycyzharry: 那请问会是什么地方出错了呢

定不负相思懿 | 园豆:132 (初学一级) | 2017-05-25 10:26

@定不负相思懿:  用我这个试试

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 | 园豆:25653 (高人七级) | 2017-05-25 11:25

@ycyzharry: 不行的,还是报错

定不负相思懿 | 园豆:132 (初学一级) | 2017-05-25 14:50

@ycyzharry: 改了一下请求,已经可以了,谢谢。不知道您能否告诉我我之前的错误

定不负相思懿 | 园豆:132 (初学一级) | 2017-05-25 15:11
其他回答(1)
0

看起来像是你进入了CORS的坑。

你不说你咋访问的,谁知道可能的错误在哪儿呢。

爱编程的大叔 | 园豆:30839 (高人七级) | 2017-05-25 12:15

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();
}

 

支持(0) 反对(0) 定不负相思懿 | 园豆:132 (初学一级) | 2017-05-25 14:31

@定不负相思懿: 我看了下你代码 主要功能是好的 去掉一些内容后是可以访问的

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();
}
}
}

支持(0) 反对(0) ycyzharry | 园豆:25653 (高人七级) | 2017-05-25 15:37

@ycyzharry: 换了一下代码,发现提示为java.io.FileNotFoundException: https://zhuanlan.zhihu.com/api/columns/zhihuadmin/posts?limit=15&offset=0,好像是IO流的问题?

支持(0) 反对(0) 定不负相思懿 | 园豆:132 (初学一级) | 2017-05-25 16:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册