對於請求:
https://graph.facebook.com/me?fields=cover&access_token=YOUR_TOKEN
JSON響應:
{
"cover": {
"id": "XXXXXXXXX",
"source": "URL_OF_COVER_PHOTO_IMAGE",
"offset_y": 50
},
"id": "XXXXXXXXX"
}
希望它可以幫助你
要解析爲JSON內容使用這個類:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, ArrayList<NameValuePair> postParameters) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//add parameters to the post request
httpPost.setHeader("Content-type", "application/json;charset=utf8");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
然後,您將以JSON字符串形式顯示響應。使用JSONObject,你將只能得到你想要的字段(帶有封面url的字段)。一旦你的鏈接,只需從網址下載位圖並顯示它。
嘿,謝謝你的回答。我在Facebook的網站上看到,但我不知道如何將它保存在JSON對象上。我的意思是,我有問題使用URL的功能來獲取Facebook答案的內容。 –
@JorgeBrage看看我上面的編輯。希望這有助於,讓我知道你的想法。 –