gpt4 book ai didi

android - Easy Facebook Android SDK - NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-11-30 03:56:39 25 4
gpt4 key购买 nike

我一直在使用这个SDK http://www.easyfacebookandroidsdk.com/将 Facebook 集成到我的应用程序中。我只想从我的应用程序更新状态。在 Android API 10 中使用此类非常有效。在使用 API 15 的手机上运行它会给我一个 android.os.NetworkOnMainThreadException。这是我的代码:

package com.my.wod;

import android.app.Activity;
import android.content.Context;
import com.main.xfit.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;


import com.facebook.android.*;

import com.facebook.android.Facebook.DialogListener;

public class PostFaceBookStatus extends Activity {


private static final String APP_ID = "ID OF MY APP";
private static final String[] PERMISSIONS = {"publish_stream" };

private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";

private Facebook facebook;
private String messageToPost;

public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}

public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.facebook);

String facebookMessage = getIntent().getStringExtra("facebookMessage");
Toast.makeText(getApplicationContext(), facebookMessage, Toast.LENGTH_LONG).show();
if (facebookMessage == null){
facebookMessage = "SUNDAY 120506 2012 CrossFit Games Regional: Individual Event 4 For time: 135 pound Back squat, 50 reps 40 Pull-ups 135 pound Shoulder-to-overhead, 30 reps 85 pound Front squat, 50 reps 40 Pull-ups 85 pound Shoulder-to-overhead, 30 reps 65 pound Overhead squat, 50 reps 40 Pull-ups 65 pound Shoulder-to-overhead, 30 reps";
}
//messageToPost = facebookMessage;
messageToPost = "Post random shit" ;
}

public void doNotShare(View button){
finish();
}
public void share(View button){
if (! facebook.isSessionValid()) {
loginAndPostToWall();
}
else {
postToWall(messageToPost);
}
}

public void loginAndPostToWall(){
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}

public void postToWall(String message){
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
showToast("Blank response.");
}
else {
showToast("Message posted to your facebook wall!");
}
finish();

} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}

class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null){
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}

private void showToast(String message){
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}

有什么想法吗?

最佳答案

您不能在 UI 线程上运行网络操作,如果您认为是 API 代码在执行此操作,那么您必须将 API 调用包装在线程中。

看起来这条线路正在使用网络:

 String response = facebook.request("me/feed", parameters, "POST");

您需要在工作线程上执行长时间运行的任务。

您过去常常遇到 ANR( Activity 无响应),但 ICS 更聪明一些,它会在这种情况发生之前将您踢走。

开始阅读:

http://developer.android.com/guide/practices/responsiveness.html

http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask <这是一个可能的备选答案

关于android - Easy Facebook Android SDK - NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13232075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com