3
//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 中得到两件事,但我无法获取这些是:-

  1. 标题:"Sun,Moon, Mars, Rahu and Jupiter Antardasha during Sun's Mahadasha"
  2. 图片:

    图片

4

1 回答 1

5

第 1 步:复制 WEBSERVICE URL 并粘贴到您的浏览器,这将访问 Web 服务并显示响应,使用 chrome 将更有助于查看 JSON 响应

第 2 步:首先分析 JSON 响应的结构,您将读取完整的响应作为字符串

从字符串创建一个 JSON OBJECT

现在将该 JSON 对象转换为 JSONARRAY 对象,

现在你有一个 JSONARRAY

迭代 JSON Array 并一一存储 Object

在 JSON 数组的迭代循环中,对于每个 JSON OBJECT,按其名称调用值,在 JSON 中看到您有键值对

您可以调用 JSONOBJECT.getString("检索字符串的变量名");

或者您也可以获得类似的其他数据类型

自己试试这个,把状态发给我,之后会用修改后的代码回复你=============================== =====================================

我试图为你解决它,这是课程

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 OBJECT

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 数组,您必须将其放入 JSON ARRAY 中,然后通过

jsonObject.getJSONARRAY("key name for JSON ARRAY IN RESPONSE ")

然后你可以遍历 JSON ARRAY 的元素

`jsonArrayObj.get(0);`

现在您可以遍历并检索您想要的值,如果需要进一步帮助,请发帖给我

于 2013-09-23T12:51:02.563 回答