मुझे निम्नलिखित कोड के साथ कुछ समस्याएं आ रही हैं। मैं निम्नलिखित 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 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;
}
डिबगिंग करते समय मुझे निम्नलिखित परिणाम मिलते हैं: कच्चा खाली रह जाता है।
क्या किसी के पास कोई सुराग है कि यह कुछ भी क्यों नहीं पढ़ रहा है? मुझे आशा है कि मैंने इसे समझने के लिए पर्याप्त कोड शामिल किया है। अगर अब और जरूरत है तो कृपया मुझे बताएं।
1 उत्तर
नमस्ते प्रतिक्रिया प्राप्त करने के लिए आपको एक 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));
}
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।
str = ""; while ((line=in.readLine()) != null) { str += line; ...
यदि आप लंबी फ़ाइल पढ़ रहे हैं तो यह बहुत बुरा विचार है। प्रत्येक बार जब आपa=a+"b"
कॉल करते हैं, तब से स्ट्रिंग परिणाम को लूप में न बनाएं क्योंकि जावा को स्ट्रिंगबिल्डर बनाने की आवश्यकता होती है जो पुराने मान की प्रतिलिपि बनाएगा, नया भाग संलग्न करेगा और फिर उस सामग्री के आधार पर नया स्ट्रिंग बनाएगा (जिसके लिए एक और पुनरावृत्ति की आवश्यकता होती है)। लूप से पहले एक स्ट्रिंगबिल्डर बनाना बेहतर है,append
लूप में सभी भाग और अंत में इसेtoSring()
में परिवर्तित करें।