我有一個活動類和登錄片段。我使用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);
}
}
使用的AsyncTask –
@SimplePlan請你將它張貼作爲一個回答你的網絡相關工作納入背景。 – VenushkaT