Facebook登录时出现NetworkOnMainThreadException

时间:2014-03-19 04:44:04

标签: android facebook android-fragments

我有一个活动类和登录片段。我使用过Facebook登录。现在我有异常叫。NetworkOnMainThreadException。请在我使用的代码下找到。

 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);
    }

}

4 个答案:

答案 0 :(得分:1)

使用Network将您的Asynchronous Task相关工作纳入背景。

有关后台处理程序的更多信息:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

并查看此SO Post

答案 1 :(得分:0)

您正在ui线程上处理网络操作。因此NetworkOnMainThreadException。您可以使用AsyncTaskThreadvolley

Fragment构造函数也应该没有参数。

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

您可以按照facebook docs @

的facebook登录进行操作

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

我猜在facebook sdk中也提供了一个示例。

答案 2 :(得分:0)

试试这种方式

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

thread.start();

答案 3 :(得分: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
            }
        };