Android 如何将图片而不是消息发布到facebook?
我用这个代码把图片上传到facebook上。但是这个代码没有响应。请帮帮我Android 如何将图片而不是消息发布到facebook?,android,Android,我用这个代码把图片上传到facebook上。但是这个代码没有响应。请帮帮我 //I had created onCreate() method as : public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); facebook = new Facebook(APP_ID); resto
//I had created onCreate() method as :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
postToWall();
}
//将此代码用于postToWall():在该方法中,我给出了要post//的图像路径,该方法正在调用onCreate()
私有void postToWall(){
//将此代码用于fbRequestListener()类:
公共类fbRequestListener实现RequestListener{
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.d("RESPONSE",""+response);
}
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
Log.d("RESPONSE",""+e);
showToast("Authentication with Facebook failed!");
finish();
}
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub
Log.d("RESPONSE",""+e);
showToast("Authentication with onFileNotFoundException failed!");
finish();
}
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub
showToast("Authentication with onMalformedURLException!");
finish();
}
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
Log.d("RESPONSE",""+e);
showToast("Authentication with onFacebookError failed!");
finish();
}
}
您必须使用GRAPH API来编写本手册或HACKBOOK 这是一个简单的方法 检查下面的代码 替换该示例中的以下代码
public static void postToWall1(String message, Context con, String url,
String Product) {
facebook1 = new Facebook(APP_ID);
Bundle parameters = new Bundle();
parameters.putString("caption", Product);
parameters.putString("description", "Share photo!");
parameters.putString("picture", "" + url);
parameters.putString("message", "" + message);
try {
facebook1.request("me");
String response = facebook1.request("me/feed", parameters, "POST");
if (response != null
|| !response.equals("")
|| !response.equals("false")
)
showToast("Message posted to your facebook wall!", con);
}
} catch (Exception e) {
showToast("Failed to post to wall!", con);
e.printStackTrace();
}
}
请尝试以下代码:
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"https://graph.facebook.com/me/photos?access_token="+ acess_token);
URL url = new URL("image_url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bi = BitmapFactory.decodeStream(input);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bi.compress(CompressFormat.PNG,100, bos);
byte[] data = bos.toByteArray();
entity.addPart("source",new ByteArrayBody(data,"testimage.png"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);
Log.v("response ", response+ "");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Hi-Adil可能重复,我必须在哪里调用postToWall()方法?在onCreat()方法中还是在哪里?请帮助我,我提供了我所提供的代码。即使我也包括Facebook SDK。。
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"https://graph.facebook.com/me/photos?access_token="+ acess_token);
URL url = new URL("image_url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bi = BitmapFactory.decodeStream(input);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bi.compress(CompressFormat.PNG,100, bos);
byte[] data = bos.toByteArray();
entity.addPart("source",new ByteArrayBody(data,"testimage.png"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);
Log.v("response ", response+ "");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}