Java 400错误,但在浏览器中打开

时间:2014-04-14 23:34:26

标签: java facebook facebook-graph-api facebook-fql

我正在制作一个facebook软件,为此我需要制作一个fql查询来获取好友列表。我使用下面的代码

String url = "https://graph.facebook.com/fql?q=SELECT uid, name, pic_square FROM  user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 =     me())&access_token="+access;
    url = url.replace(" ", "%20");
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("POST");
    //add request header
    String USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
    con.setRequestProperty("User-Agent", USER_AGENT);
    int responseCode = con.getResponseCode();
    System.out.println(responseCode);
    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }

    in.close();

但我似乎总是得到400错误。我尝试用%20替换空格但没有效果。 当我在浏览器中打开链接时,它会打开。

1 个答案:

答案 0 :(得分:1)

我感到惭愧。解决方案实际上是POST线,应该是GET。

String url = "https://graph.facebook.com/fql?q=SELECT uid, name, pic_square FROM  user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 =     me())&access_token="+access;
url = url.replace(" ", "%20");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
String USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println(responseCode);
BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}

in.close();