我希望我的应用程序只显示 facebook 页面发布的帖子而无需任何身份验证。这是代码:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String APP_ID = "1234567890";
String APP_SECRET = "1a2b3c4defghijk";
String OWNER_OF_FEED = "somepage";
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(
"https://graph.facebook.com/oauth/access_token?client_id="+
APP_ID +
"&client_secret="+APP_SECRET+"&grant_type=client_credentials");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String access_token = client.execute(get, responseHandler);
String uri = "https://graph.facebook.com/" + OWNER_OF_FEED + "/feed?"
+ access_token;
get = new HttpGet(uri);
String responseBody = client.execute(get, responseHandler);
// responseBody contains JSON-encoded feed
//textview.setText(responseBody);
TextView txtv = (TextView) findViewById(R.id.feed);
txtv.setText(String.valueOf(responseBody));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
但我无法在应用程序上获取任何内容。我已经在清单文件中包含了互联网访问权限。
新代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URL url;
try {
url = new URL("https://www.facebook.com/feeds/page.php?format=rss20&id=289781571139242");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}