我需要做些什么来获取userId,facebook个人资料照片,emailid和当前登录用户的地址。
一旦app授权用户登录,我将在异步任务中调用以下方法,但它失败并抛出异常: -
“android.os.NetworkOnMainThreadException”
请查看我在asynctask的doInBackground()中执行的方法: -
private void callFBMethods() {
try {
response = facebook.request("/me");
if(response!=null)
{
handler.sendEmptyMessage(0);
}
JSONObject json = Util.parseJson(response);
mainURL = Constant.FB_PIC_URL+json.getString("id")+"/picture?type=small";
//To registere new user here
Vector<Object> userData = new Vector<Object>();
userData.add(Constant.CURRENT_FLAG);
userData.add(json.getString("username"));
userData.add(json.getString("name"));
userData.add(json.getString("id"));
userData.add(mainURL);
new AsynServices(mActivity, userData, 4).execute();
} catch (Exception e) {
// TODO: handle exception
System.out.println("exception occurs:-"+e.getMessage());
}
}
答案 0 :(得分:0)
NetworkOnMainThreadException
此错误因为从Android API 14开始,您必须运行与Internet相关的代码应该在AsyncTask中。像这样你可以在AsyncTask
private class Task extends AsyncTask<Void, Void, Void> {
@Override
protected String doInBackground(String... urls) {
// call your methods from here
try {
response = facebook.request("/me");
if(response!=null)
{
handler.sendEmptyMessage(0);
}
JSONObject json = Util.parseJson(response);
mainURL = Constant.FB_PIC_URL+json.getString("id")+"/picture?type=small";
//To registere new user here
Vector<Object> userData = new Vector<Object>();
userData.add(Constant.CURRENT_FLAG);
userData.add(json.getString("username"));
userData.add(json.getString("name"));
userData.add(json.getString("id"));
userData.add(mainURL);
// instead of calling another Asynctask here just write related code here.
new AsynServices(mActivity, userData, 4).execute();
} catch (Exception e) {
// TODO: handle exception
System.out.println("exception occurs:-"+e.getMessage());
}
}
}