0

I have one activity class and login fragment .there I have used Facebook login.Now Im having exception called.NetworkOnMainThreadException .please find below the code I used.

 package com.maintab;
import org.json.JSONObject;
import com.example.tesfragement.*;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.service.textservice.SpellCheckerService.Session;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;


@SuppressLint("ValidFragment")
public class Login extends Fragment   {

    View view;
    ImageView loginbuttton,logoutbutton;

    Facebook fb;

    @SuppressLint("ValidFragment")
    public Login(Facebook fb) {

        this.fb=fb;
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.login, container, false);
        loginbuttton=(ImageView) view.findViewById(R.id.loinbutton);

        thread.start();

        return view;
    }


    Thread thread=new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            loginbuttton.setOnClickListener(btnClick);
        }
    });


    ImageView.OnClickListener btnClick=new ImageView.OnClickListener()
    {



        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


                 if(fb.isSessionValid())
                 {
                     Toast.makeText(getActivity(),"session is valid", Toast.LENGTH_LONG).show();
                 }
                 else{

                        fb.authorize(getActivity(), new String[]{"email"}, new DialogListener() {

                            @Override
                            public void onFacebookError(FacebookError e) {

                                Toast.makeText(getActivity(), "Network Connction Unavaliable", Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onError(DialogError e) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onComplete(Bundle values) {
                                // TODO Auto-generated method stub

                                 try {
                                        JSONObject json = Util.parseJson(fb.request("me"));
                                        String firstName = json.getString("first_name");
                                        String lastName = json.getString("last_name");

                                        String full_name = firstName+" "+lastName;
                                        Log.d("Name", firstName+" "+lastName);

                                        Toast.makeText(getActivity(), "You already have a valid session, " + firstName + " " + lastName + ". No need to re-authorize.", Toast.LENGTH_SHORT).show();
                                        }catch( Exception error ) {
                                            Toast.makeText( getActivity(), error.toString(), Toast.LENGTH_SHORT).show();
                                            }

                            }


                            @Override
                            public void onCancel() {
                                // TODO Auto-generated method stub
                                Toast.makeText(getActivity(), "Login Error...", Toast.LENGTH_LONG).show();
                            }
                        });

                    }




        }

    };
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }

}
VenushkaT
  • 1,152
  • 2
  • 19
  • 42

4 Answers4

1

do your Network related work into Background using Asynchronous Task.

For more information about Background Handler: http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

and also take a look into this SO Post

Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
0

You are processing Network operation on the ui thread. Hence the NetworkOnMainThreadException. You could use AsyncTask, Thread or volley.

Also Fragment constructor should have no arguments.

public Login(Facebook fb) { // constructor has an arg

You could follow the facebook login for android docs @

https://developers.facebook.com/docs/android/login-with-facebook/

There is a sample also provided in the facebook sdk i guess.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Try this way

Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            //Your code goes here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start();
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
0

Handler myHandler=new Handler(); Runnable runnable;

   new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    //this is background thread.
                } catch (Exception e) {

                }
                myHandler.post(runnable);
            }
        }).start();
        runnable = new Runnable() {

            @Override
            public void run() {
                //This is UI thread.U can add UI related code here
            }
        };
anjaneya
  • 708
  • 9
  • 11