Facebook sdk encoded for android 2.2 does not work on android 4.0.4 showing android.os.Networkonmainthread error

Hi, I am working on facebook api. I looked through the documentation on facebook and encoded according to the instructions in it. The code below works fine in android 2.1-3.2, but while working in android 4.0.4 it gives android.os.networkonmainthread error. I do not understand how to write the code below using AsynckTask or threads (Painless thread). Please, everything is here. Suggest me how to code so that it can run on all versions of android.Here my code.

public class Facebook4 extends Activity { public static final String mAPP_ID = "222299217893177"; public Facebook mFacebook = new Facebook(mAPP_ID); private static final String FACEBOOK_PERMISSION = "publish_stream"; private SharedPreferences mPrefs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_facebook4); ((ImageView)findViewById(R.id.facebookLogin)).setOnClickListener( loginButtonListener ); SessionStore.restore(mFacebook, this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { mFacebook.authorizeCallback(requestCode, resultCode, data); } private OnClickListener loginButtonListener = new OnClickListener() { public void onClick( View v ) { if( !mFacebook.isSessionValid() ) { Toast.makeText(Facebook4.this, "Authorizing", Toast.LENGTH_SHORT).show(); mFacebook.authorize(Facebook4.this, new String[] {FACEBOOK_PERMISSION},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener()); //startActivity(new Intent(FacebookLogin.this,MainActivity.class)); } else { Toast.makeText( Facebook4.this, "Has valid session", Toast.LENGTH_SHORT).show(); try { JSONObject json = Util.parseJson(mFacebook.request("me")); String facebookID = json.getString("id"); String firstName = json.getString("first_name"); String lastName = json.getString("last_name"); Toast.makeText(Facebook4.this, "You already have a valid session, " + firstName + " " + lastName + ". No need to re-authorize.", Toast.LENGTH_SHORT).show(); } catch( Exception error ) { Toast.makeText( Facebook4.this, error.toString(), Toast.LENGTH_SHORT).show(); } } //startActivity(new Intent(FacebookLogin.this,MainActivity.class)); } }; public final class LoginDialogListener implements DialogListener { public void onComplete(Bundle values) { try { //The user has logged in, so now you can query and use their Facebook info JSONObject json = Util.parseJson(mFacebook.request("me")); Log.i("id", json.getString("id")); Log.i("FirstName", json.getString("first_name")); Log.i("lastname", json.getString("last_name")); Log.i("email", json.getString("email")); Log.i("Name", json.getString("name")); Log.i("gender",json.getString("gender")); SessionStore.save(mFacebook, Facebook4.this); } catch( Exception error ) { Toast.makeText( Facebook4.this, error.toString(), Toast.LENGTH_SHORT).show(); Log.i("error", error.toString()); } } public void onFacebookError(FacebookError error) { Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show(); } public void onError(DialogError error) { Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show(); } public void onCancel() { Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show(); } } 
+4
source share
3 answers

You get this error because you are performing a network operation, for example, accessing Facebook in the main topic of the user interface.

To fix this best, move all network-related code to a separate Thread or AsyncTask

+1
source

I have the same problem that I do when I check my manifest as it was

  <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> 

I deleted targetSdkVersion

  <uses-sdk android:minSdkVersion="8" /> 

and it worked, I think it will help you try

+3
source

In the top api, now you can not perform the network task in the main thread. Run your code in another thread and no exception will appear.

0
source

Source: https://habr.com/ru/post/1441536/


All Articles