我在这里遇到这种情况:
我的LoginActivity,从FacebookActionBarActivity扩展,从名为Session的自定义类的实例运行login方法:
@Override
protected void onFacebookLogin(final GraphUser facebookUser, com.facebook.Response response) {
User user = new User(this, facebookUser);
Session preSession = new Session(this, user);
preSession.setCallback(new Response() {
@Override
public void onSuccess(Message m) {
Log.i("LoginOk", "Yeaaaaaaaaah!: " + ((Session) m.getData()).toQueryString());
hjClient.setSession((Session) m.getData());
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.putExtra("hjClient", hjClient);
startActivity(i);
}
@Override
public void onError(Message m) {
//closeFacebookSession();
if (m.getData() instanceof Exception) {
((Exception) m.getData()).getStackTrace();
} else {
Log.i("onError", m.getData().toString());
Toast.makeText(LoginActivity.this, m.getDescription(), Toast.LENGTH_LONG);
}
}
});
preSession.login();
}
Session.login()是这样的:
public void login() {
RequestAsyncTask request = new RequestAsyncTask(getContext(), new Response() {
@Override
public void onSuccess(Message m) {
try {
Log.i("onSuccess login", m.getData().toString());
JSONObject jsonObject = new JSONObject(m.getData().toString());
// Si no hay un atributo code o el atributo code es 0 entonces el mensaje
// desde el servicio es un OK
if (!jsonObject.has("code") || jsonObject.get("code").equals("0")) {
if (getCallback() != null) getCallback().onSuccess(new Message(new Session(jsonObject)));
} else {
onError(new Message(jsonObject));
}
} catch (Exception e) {
onError(new Message(e));
}
}
@Override
public void onError(Message m) {
if (getCallback() != null) getCallback().onError(m);
}
});
request.execute("method=sessionLogin&" + toQueryString());
}
它创建一个RequestAsyncTask实例。 RequestAsyncTask从Java本机AsyncTask扩展并处理响应消息。这是RequestAsyncTask:
public class RequestAsyncTask extends AsyncTask {
Context context;
String url;
List<NameValuePair> params;
Response callback;
// constructor
public RequestAsyncTask(Context context, Response callback) {
super();
setContext(context);
setCallback(callback);
}
// methods
@Override
protected Object doInBackground(Object[] params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(getUrl());
setParams(params[0].toString());
post.setEntity(new UrlEncodedFormEntity(getParams()));
return client.execute(post);
} catch (Exception e) {
// This Exception will be catched on onPostExecute and it will throw another Exception
return e;
}
}
@Override
protected void onPostExecute(Object o) {
if (getCallback() != null) {
Log.i("postExecute", o.toString());
if (!(o instanceof Exception)) {
try {
String httpResponse = new BasicResponseHandler().handleResponse((HttpResponse) o);
//JSONObject jsonResponse = new JSONObject(httpResponse);
//getCallback().onSuccess(new Message(jsonResponse));
Log.i("httpResponse", httpResponse);
getCallback().onSuccess(new Message(httpResponse));
} catch (Exception e) {
Log.i("postExecuteRrror", e.toString());
getCallback().onError(new Message(e));
}
} else {
Log.i("Error en llamada", ":D");
getCallback().onError(new Message((Exception) o));
}
}
}
我的问题是我在登录方法中启动的RequestAsyncTask实例的PostExecute方法上收到了NetworkOnMainThreadException。
这是StackTrace:
04-26 14:08:52.571 9552-9552/net.agurodriguez.hoyjugamos I/FacebookActionBarActivity﹕ Logged in...
04-26 14:08:52.821 9552-9552/net.agurodriguez.hoyjugamos E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 14:08:52.821 9552-9552/net.agurodriguez.hoyjugamos E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-26 14:08:56.025 9552-9552/net.agurodriguez.hoyjugamos I/queryString:﹕ user_birthday=1991-01-03&user_email=agurodriguez@icloud.com&user_firstName=Federico&user_id=10206493058584965&user_lastName=Rodríguez&user_middleName=Agustín&user_sex=1
04-26 14:08:56.025 9552-9552/net.agurodriguez.hoyjugamos I/queryString:﹕ deviceId=3febed0b2be4bcb7&deviceManufacturer=samsung&deviceModel=GT-S7390L&deviceOS=Android Unknown 1&id=0&user_birthday=1991-01-03&user_email=agurodriguez@icloud.com&user_firstName=Federico&user_id=10206493058584965&user_lastName=Rodríguez&user_middleName=Agustín&user_sex=1
04-26 14:08:56.035 9552-9552/net.agurodriguez.hoyjugamos I/MeRequest﹕ GraphObject{graphObjectClass=GraphUser, state={"id":"10206493058584965","first_name":"Federico","birthday":"01\/03\/1991","timezone":-3,"email":"agurodriguez@icloud.com","verified":true,"middle_name":"Agustín","name":"Federico Agustín Rodríguez","locale":"es_LA","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/10206493058584965\/","last_name":"Rodríguez","gender":"male","updated_time":"2014-06-12T02:59:52+0000"}}
04-26 14:08:57.346 9552-9552/net.agurodriguez.hoyjugamos I/postExecute﹕ org.apache.http.message.BasicHttpResponse@425125b0
04-26 14:08:57.346 9552-9552/net.agurodriguez.hoyjugamos I/postExecuteRrror﹕ android.os.NetworkOnMainThreadException
我做错了什么?有人能帮助我吗?
答案 0 :(得分:0)
首先,这不是Java堆栈跟踪。
其次,将所有您的网络I / O移至doInBackground()
。这包括您的handleResponse()
电话。