//My API link
//http://gdata.youtube.com/feeds/base/videos?max-results=10&start-//index=1&alt=json&orderby=published&author=astrobixweb
//String Method to fetech data from server
public static String sendRequest(String url) {
String result = "";
try {
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,
"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while (true) {
s = buf.readLine();
if (s == null || s.length() == 0)
break;
sb.append(s);
}
buf.close();
ips.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
//Here is parser class
public static void GroupResult(String url){
try{
JSONArray jsonarray,jsonArray1,jsonArray2 ;
JSONObject json ;
response=GetJsonObject.sendRequest(url);
//data comes into response variable
if(response == null){
return;
}
jsonarray = new JSONArray("["+response+"]");
json = jsonarray.getJSONObject(0);
String feed = (json.getString("feed"));
Log.v("feed", ""+feed);
//try{
jsonarray = new JSONArray("["+feed+"]");
json = jsonarray.getJSONObject(0);
String entry = json.getString("entry");
jsonarray = new JSONArray(entry);
for (int i = 0; i < jsonarray.length(); i++)
{
mData=new AstrobixData();
json = jsonarray.getJSONObject(i);
String title_array = json.getString("title");
jsonArray1 = new JSONArray("["+title_array+"]");
String title = jsonArray1.getJSONObject(0).getString("$t");
String imagepath=json.getString("content");
jsonArray2=new JSONArray("["+imagepath+"]");
String urliamge=jsonArray1.getJSONObject(0).getString("$t");
}
// mData.SetTitle(title);
// mList.add(mData);
}
}
// Log.v("title", ""+title_list);
}
}
请哪位帮忙把这个API链接的数据拿过来。我必须尝试,我必须通过 http 获取 String 变量中的所有数据。但我想从这个 API 做两件事 但我无法获取这些:-
- title:
“Sun, Moon, Mars, Rahu and Jupiter Antardasha during Sun's 摩诃达沙”
图片:
最佳答案
第 1 步:复制 WEBSERVICE URL 并粘贴到您的浏览器中,这将访问 Web 服务并显示响应,使用 chrome 会更有帮助地查看 JSON 响应
第 2 步:分析 JSON 响应的结构 首先,您将阅读作为字符串的完整响应
从字符串创建一个 JSON 对象
现在将该 JSON 对象转换为 JSONARRAY 对象,
现在你有了一个 JSONARRAY
遍历JSON数组并逐一存储Object
在 JSON 数组的迭代循环中,对于每个 JSON OBJECT 调用它们的值 姓名 在 JSON 中看到你有键值对
可以调用JSONOBJECT.getString("获取字符串的变量名");
或者你也可以获得类似这样的其他数据类型
自己试试吧,把状态发给我,我会用修改后的代码回复你 然后 ================================================ =================
我试着为你解决了,这是类(class)
package com.hussain.StackOverFlow;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class FarhaSameer1 {
public static void main(String[] args)
{
String asd = FarhaSameer1.sendRequest("http://gdata.youtube.com/feeds/base/videos?max-results=10&start-//index=1&alt=json&orderby=published&author=astrobixweb");
FarhaSameer1.parseFromJSONResponse(asd);
}
// API link
// http://gdata.youtube.com/feeds/base/videos?max-results=10&start-//index=1&alt=json&orderby=published&author=astrobixweb
// String Method to fetech data from server
public static String sendRequest(String url) {
String result = "";
try {
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while (true) {
s = buf.readLine();
if (s == null || s.length() == 0)
break;
sb.append(s);
}
buf.close();
ips.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void parseFromJSONResponse(String respo)
{
JSONObject myjson;
try
{
myjson = new JSONObject(respo);
JSONObject jsonObj1 = myjson.getJSONObject("feed");
JSONArray jsonObj2 = jsonObj1.getJSONArray("entry");
JSONObject jsonObj3 = jsonObj2.getJSONObject(0);
System.out.println(jsonObj3.getJSONObject("content"));
System.out.println("here ===>>>"+jsonObj3.getJSONObject("content").get("$t").toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
看第一个方法和你写的一样 在第二种方法中,我试图逐步遍历 JSON 响应。 看到你必须小心你的 JSON 响应
1:您的完整响应是一个 JSON 对象
2 :如果任何元素写成
"some key name " : { " some value " }
这是一个 JSON 对象
3 : 如果任何元素写成这样
"some key name " : " some value "
这是你json对象中的值,你可以通过
jsonObject.getString("key name")
4 : 如果任何元素写成
"some key name " : [ " some value " ]
然后这是一个JSON Array,你必须把它带入一个JSON ARRAY,然后遍历它的元素
jsonObject.getJSONARRAY("key name for JSON ARRAY IN RESPONSE ")
然后可以通过
遍历JSON ARRAY的元素`jsonArrayObj.get(0);`
现在您可以遍历并检索您想要的值,如果需要进一步的帮助,请发邮件给我
关于java - 如何通过JSONArray获取API的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18957450/