2014-03-19 65 views
0

我有一個活動類和登錄片段。我使用Facebook登錄。現在我有異常調用。 NetworkOnMainThreadException。請在下面找到我使用的代碼。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); 
    } 

} 
+0

使用的AsyncTask –

+0

@SimplePlan請你將它張貼作爲一個回答你的網絡相關工作納入背景。 – VenushkaT

回答

0

您正在處理ui線程上的網絡操作。因此NetworkOnMainThreadException。您可以使用AsyncTaskThreadvolley

另外Fragment的構造函數應該沒有參數。

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

你可以請按照Android文檔的Facebook登錄@

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

有一個在Facebook的SDK我猜還提供了一個樣本。

+0

請發現我編輯的代碼即時獲取相同的異常 – VenushkaT

+0

@VenushkaT簡單。按照文檔鏈接張貼在我的文章 – Raghunandan

+0

我已按照它,但註銷按鈕沒有對我工作 – VenushkaT

0

嘗試這種方式

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

thread.start(); 
+0

我已done.please找到我編輯code.still即時獲得exception.thank你 – VenushkaT

0

處理程序將myHandler =新的處理程序(); 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 
      } 
     }; 
+0

仍然即時獲得相同例外 – VenushkaT

+0

把這個上面的代碼在方法例如load()和調用該方法在Oncreate ... – anjaneya

+0

它didn'對我起作用。我試過了,你可以編輯我的code.please – VenushkaT