我正在努力改进我的设计并把东西放在适当的位置。所以我想放弃把我的逻辑放在我的活动中。为此,我创建了以下类:
public class FacebookLoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_login_activity);
new FacebookName().execute(this);
}
private class FacebookName extends AsyncTask<Activity, Void, String> {
@Override
protected String doInBackground(Activity... params) {
CustomStatusCallback customStatusCallback = new CustomStatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
CustomGraphUserCallback customerGraphUserCallback = new CustomGraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
setResult(user.getName());
}
}
};
Request.newMeRequest(session, customerGraphUserCallback).executeAndWait();
}
}
};
Session.openActiveSession(params[0], true, customStatusCallback);
return customStatusCallback.getResult();
}
@Override
protected void onPostExecute(String result) {
TextView facebookName = (TextView) findViewById(R.id.facebookName);
facebookName.setText("Hi " + result);
}
};
}
以下是我创建的其他自定义类,用于保存值并将其传递回protected void onPostExecute(String result)
:
public abstract class CustomGraphUserCallback implements Request.GraphUserCallback {
private String result;
public String getResult() {
return result;
}
protected void setResult(String result) {
this.result = result;
}
}
这是我的CustomStatusCallback:
public abstract class CustomStatusCallback implements StatusCallback {
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
我收到以下异常:
02-28 21:32:08.781: E/AndroidRuntime(15215): android.os.NetworkOnMainThreadException
02-28 21:32:08.781: E/AndroidRuntime(15215): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
02-28 21:32:08.781: E/AndroidRuntime(15215): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-28 21:32:08.781: E/AndroidRuntime(15215): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-28 21:32:08.781: E/AndroidRuntime(15215): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
02-28 21:32:08.781: E/AndroidRuntime(15215): at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Response.fromHttpConnection(Response.java:301)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeConnectionAndWait(Request.java:1564)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeBatchAndWait(Request.java:1463)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeBatchAndWait(Request.java:1432)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeBatchAndWait(Request.java:1414)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeAndWait(Request.java:1386)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.facebook.Request.executeAndWait(Request.java:1268)
02-28 21:32:08.781: E/AndroidRuntime(15215): at com.blahblah.FacebookLoginActivity$FacebookName$1.call(FacebookLoginActivity.java:47)
你能帮帮我吗?同样,我知道如何使用executeAsync()
使其工作并将所有内容放在主要操作中。
先谢谢男女老少:)
答案 0 :(得分:0)
以下是您的表现方式:
private class FBUserInfoTask extends AsyncTask<Session, Void, Map<String, Object>>
{
@Override
protected Map<String, Object> doInBackground(Session[] session) // session state should be OPENED
{
Request request = new Request(session[0], "me"); //use any graph api here instead of "me"
Response response = request.executeAndWait();
GraphObject obj = response.getGraphObject();
Map<String, Object> map = obj.asMap();
return map;
}
@Override
protected void onPostExecute(Map<String, Object> result)
{
super.onPostExecute(result);
Log.d("User Name:", (String) result.get("userId"));
}
}