我正在尝试在Facebook Graph API调用中进行HTTP POST。这是我的代码。当我在Facebook API调用之外调用函数executeHTTP()
时,代码正在工作。如果我将executeHTTP()
放在Facebook调用的onCompleted()
函数内,则会显示URL not found错误。
GraphRequest graphRequest = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/"+photoID,
null,
HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
// TODO Auto-generated method stub
//Log.d("Photo Response ","Response " + response);
JSONObject json = response.getJSONObject();
try{
//Log.d("Photo", json.getString("picture"));
// Store the photo id and url into the hashmap
photoURLS.put(json.getString("id"), json.getString("picture"));
noOfRequest--;
// HTTP Post
executeHTTP(); //****** THIS LINE
}
catch(JSONException e){
//
noOfRequest--;
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "picture");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
我希望在完成Facebook调用后调用函数executeHTTP()
(处理HTTP POST)。可能吗?如果是这样,怎么样?我错过了代码中的任何内容吗?
public void executeHTTP(){
// execute http post here
for (Map.Entry<String, String> entry : photoURLS.entrySet()) {
Log.d(entry.getKey() , entry.getValue());
}
// HTTP POST
JSONObject data = new JSONObject(photoURLS); // convert the hashmap into json object
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("json",data.toString()));
// Create a new HttpClient and Post Header
String response = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.geoamy.com/uploadImages.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("PHP", "POST Response >>> " + response);
}
编辑:这就是我得到的。日志
没有错误09-02 19:30:28.500: D/PHP(374): POST Response >>> <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
09-02 19:30:28.500: D/PHP(374): <html><head>
09-02 19:30:28.500: D/PHP(374): <title>403 Forbidden</title>
09-02 19:30:28.500: D/PHP(374): </head><body>
09-02 19:30:28.500: D/PHP(374): <h1>Forbidden</h1>
09-02 19:30:28.500: D/PHP(374): <p>You don't have permission to access /uploadImages.php
09-02 19:30:28.500: D/PHP(374): on this server.</p>
09-02 19:30:28.500: D/PHP(374): <p>Additionally, a 404 Not Found
09-02 19:30:28.500: D/PHP(374): error was encountered while trying to use an ErrorDocument to handle the request.</p>
09-02 19:30:28.500: D/PHP(374): </body></html>