从freebase - Java请求大量数据

时间:2013-04-14 05:45:01

标签: java freebase

我试图从freebase请求大量数据。但我收到错误消息“HTTP响应代码:403”。以前有没有人有类似的问题?

这是我的代码

private static final String service_url =“https://www.googleapis.com/freebase/v1/mqlread”;

public static void main(String[] args) 
{
    try
    {
        String cursor = "";
        String urlStr_initial = service_url + "?query=" + URLEncoder.encode(getQuery(), "UTF-8") + "&cursor";
        URL url = new URL(urlStr_initial);
        List<Freebase> list = new ArrayList<Freebase>();
        Freebase f_b;
        do
        {
            HttpURLConnection  url_con = (HttpURLConnection)url.openConnection();
            url_con.addRequestProperty("User-Agent", "Mozilla/4.76");

            StringBuilder str_builder = new StringBuilder();        
            BufferedReader reader = new BufferedReader(new InputStreamReader(url_con.getInputStream()));

            String line;
            while((line = reader.readLine()) != null)
            {
                str_builder.append(line);
            }
            String response = str_builder.toString();
            JSONObject j_object = new JSONObject(response);

            if(j_object.has("result"))
            {
                JSONArray j_array = j_object.getJSONArray("result");
                for(int i = 0; i < j_array.length(); i++)
                {
                    JSONObject j_o = j_array.getJSONObject(i);
                    if(j_o.has("id") == true && j_o.has("name"))
                    {
                        String id = j_o.getString("id");
                        String name = j_o.getString("name");
                        System.out.println("ID: " + id + "   /   Name:" + name);
                        f_b = new Freebase(id, name);
                        if(f_b != null)
                        {
                            list.add(f_b);
                        }
                        else
                        {
                            System.out.println("Null value in Freebase");
                        }
                    }
                }
            }
            else
            {
                System.out.println("There is no \"result\" key in JASON object");
            }

            if(j_object.has("cursor"))
            {
                cursor = j_object.get("cursor").toString();
            }
            else
            {
                cursor = "false";
            }

            String urlStr = urlStr_initial + "=" + cursor;
            url = new URL(urlStr);
        }while( !cursor.equalsIgnoreCase("false"));

        if(list != null)
        {
            TextFile tf = new TextFile();
            tf.writeToFile(list);
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

private static String getQuery()
{
    return "[{"
            + "\"name\": null,"
            + "\"id\": null,"
            + "\"type\":\"/people/person\","
            + "\"limit\": 500"
            + "}]";
}

1 个答案:

答案 0 :(得分:1)

您没有说“大”是什么,但API并非专为批量下载而设计。您应该使用数据转储。

HTTP响应代码中通常包含更详细的错误消息。例如,如果它显示403 - Forbidden - 需要API密钥,则表示您没有包含API密钥(我没有看到您在代码中包含它的位置)。如果它显示403 - 禁止 - 超出配额意味着您已超出您的请求配额(您可以在API控制台上查看剩余的配额)。