Java 如何在Android中从URL读取json数据
我对以下代码有一些问题。我正在尝试读取以下Reddit URLJava 如何在Android中从URL读取json数据,java,android,json,url,Java,Android,Json,Url,我对以下代码有一些问题。我正在尝试读取以下Reddit URL https://www.reddit.com/r/earthporn/.json?after= 下面是未正确执行的代码 public static String readContents(String url){ try{ InputStream input= new URL(url).openStream(); Reader reader = new InputStreamRead
https://www.reddit.com/r/earthporn/.json?after=
下面是未正确执行的代码
public static String readContents(String url){
try{
InputStream input= new URL(url).openStream();
Reader reader = new InputStreamReader(input);
BufferedReader in = new BufferedReader(reader);
String line, str;
str = "";
while ((line=in.readLine()) != null) {
str += line;
System.out.println(line);
}
return str;
}catch(IOException e){
Log.d("READ FAILED", e.toString());
return null;
}
}
}
这就是我所说的
List<Post> fetchPosts(){
String raw=RemoteData.readContents(url);
List<Post> list=new ArrayList<Post>();
try{
JSONObject data=new JSONObject(raw).getJSONObject("data");
JSONArray children=data.getJSONArray("children");
//Using this property we can fetch the next set of
//posts from the same subreddit
after=data.getString("after");
for(int i=0;i<children.length();i++){
JSONObject cur=children.getJSONObject(i)
.getJSONObject("data");
Post p=new Post();
p.title=cur.optString("title");
p.url=cur.optString("url");
p.numComments=cur.optInt("num_comments");
p.points=cur.optInt("score");
p.author=cur.optString("author");
p.subreddit=cur.optString("subreddit");
p.permalink=cur.optString("permalink");
p.domain=cur.optString("domain");
p.id=cur.optString("id");
p.thumbnail=cur.optString("thumbnail");
if(p.title!=null)
list.add(p);
}
}catch(Exception e){
Log.e("fetchPosts()",e.toString());
}
return list;
}
List fetchPosts(){
String raw=RemoteData.readContents(url);
列表=新的ArrayList();
试一试{
JSONObject数据=新的JSONObject(原始).getJSONObject(“数据”);
JSONArray children=data.getJSONArray(“children”);
//使用此属性,我们可以获取下一组
//来自同一子Reddit的帖子
after=data.getString(“after”);
对于(int i=0;iHello)要获得响应,您必须打开一个HttpsURLConnection。您遇到的问题是从URL获得一个空响应,因此您必须在这里提供适用于我的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class main {
public static String getJSON(String url) {
HttpsURLConnection con = null;
try {
URL u = new URL(url);
con = (HttpsURLConnection) u.openConnection();
con.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (con != null) {
try {
con.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return null;
}
public static void main(String[] args) {
String url = "https://www.reddit.com/r/earthporn/.json?after=";
System.out.println(getJSON(url));
}
}
虽然((line=in.readLine())!=null){str+=line;…
如果您正在读取长文件,这是一个非常糟糕的主意。不要使用串联构建字符串,因为每次调用a=a+“b”时都会导致循环
Java需要创建StringBuilder,它将复制旧值,附加新部分,然后基于该内容创建新字符串(这需要另一次迭代)。最好在循环之前创建一个StringBuilder,append
循环中的所有部分,最后将其转换为toString()
。您收到的是一个特定的错误,还是它只是空的?由于HttpClient已被弃用,如何使用URLConnection;工作非常完美!!谢谢您为什么我们需要在sb.append(line+“\n”)中使用“\n”?sb.append(line)有什么问题吗。