【问题标题】:AsyncTask task onPostExecute() not executing? Tried all answer already present on stackoverflowAsyncTask 任务 onPostExecute() 没有执行?尝试了stackoverflow上已经存在的所有答案
【发布时间】:2016-01-29 21:13:04
【问题描述】:

您好开发者和朋友们, 我正在开发一个需要读取 JSON url 的 android 应用程序。我正在使用异步任务。但我不知道为什么 onPostExecute() 没有执行。我成功解析了json。唯一剩下的就是返回字符串(解析 json 文本)。

这是我的代码:

package com.vijay.jsonwizard.demo.activities;
import android.content.Intent; 
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.vijay.jsonwizard.demo.R;
import com.vijay.jsonwizard.activities.JsonFormActivity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

 public class MainActivity extends ActionBarActivity {
 private static final int REQUEST_CODE_GET_JSON = 1;
 private static final String TAG = "MainActivity";
 private static final String DATA_JSON_PATH = "http://jatinderbhola.in/phppractice/data.json";

 String json;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  findViewById(R.id.button_start).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, JsonFormActivity.class);
    BackgroundTask task = new BackgroundTask();
    task.execute();
    if (json != null) {
     intent.putExtra("json", json);
     startActivityForResult(intent, REQUEST_CODE_GET_JSON);
    } else {
     Toast.makeText(getApplicationContext(), "Error!!", Toast.LENGTH_LONG).show();
    }
   }
  });
 }

private class BackgroundTask extends AsyncTask < String, String, String > {
      @Override
      protected void onPreExecute() {
   super.onPreExecute();
  }
  @Override
  protected String doInBackground(String...params) {
   OkHttpClient client = new OkHttpClient();
   Request request = new Request.Builder()
    .url(DATA_JSON_PATH)
    .build();

   Response response = null;
   try {
    response = client.newCall(request).execute();

   } catch (IOException e) {
    e.printStackTrace();
   }

   try {
    //Log.d("Respose", response.body().string());
    return response.body().string();
   } catch (IOException e) {
    e.printStackTrace();
   }
   return null;
  }
  @Override
  protected void onPostExecute(String data) {
   set_json(data);
  }
 }

 private void set_json(String s) {
  // json = s;
  Toast.makeText(getApplicationContext(), "I'm in!!", Toast.LENGTH_LONG).show();
 }
}

谢谢并提前。

也不例外: 这是 android 监视器显示的内容:

01-28 13:42:17.117 1450-1450/com.vijay.jsonwizard.demo I/art: Not late-enabling -Xcheck:jni (already on)
01-28 13:42:17.117 1450-1450/com.vijay.jsonwizard.demo I/art: Late-enabling JIT
01-28 13:42:17.119 1450-1450/com.vijay.jsonwizard.demo I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
01-28 13:42:17.147 1450-1450/com.vijay.jsonwizard.demo W/System: ClassLoader referenced unknown path: /data/app/com.vijay.jsonwizard.demo-1/lib/x86
01-28 13:42:17.309 1450-1477/com.vijay.jsonwizard.demo D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-28 13:42:17.411 1450-1477/com.vijay.jsonwizard.demo I/OpenGLRenderer: Initialized EGL, version 1.4
01-28 13:42:17.805 1450-1458/com.vijay.jsonwizard.demo W/art: Suspending all threads took: 31.806ms
01-28 13:42:17.935 1450-1477/com.vijay.jsonwizard.demo W/EGL_emulation: eglSurfaceAttrib not implemented
01-28 13:42:17.935 1450-1477/com.vijay.jsonwizard.demo W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7600e0, error=EGL_SUCCESS
01-28 13:42:17.991 1450-1450/com.vijay.jsonwizard.demo I/Choreographer: Skipped 38 frames!  The application may be doing too much work on its main thread.
01-28 13:43:25.890 1450-1458/com.vijay.jsonwizard.demo W/art: Suspending all threads took: 5.563ms
01-28 13:49:09.646 1450-1458/com.vijay.jsonwizard.demo W/art: Suspending all threads took: 6.337ms
01-28 13:56:08.565 1450-1477/com.vijay.jsonwizard.demo W/EGL_emulation: eglSurfaceAttrib not implemented
01-28 13:56:08.566 1450-1477/com.vijay.jsonwizard.demo W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7603e0, error=EGL_SUCCESS
01-28 13:56:11.892 1450-1477/com.vijay.jsonwizard.demo E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4053c00

【问题讨论】:

  • 任何异常或错误?
  • 不!!...我更新了帖子。
  • Log.d("Respose", response.body().string());取消注释时它会执行吗?
  • 确保清单中的 Internet 权限-
  • 尝试在onPostExecute方法中写入日志,看看是否被调用。

标签: java android json parsing android-asynctask


【解决方案1】:

将下一个代码移至 PostExecute 阶段:

Intent intent = new Intent(MainActivity.this, JsonFormActivity.class);
if (json != null) {
 intent.putExtra("json", json);
 startActivityForResult(intent, REQUEST_CODE_GET_JSON);
} else {
 Toast.makeText(getApplicationContext(), "Error!!", Toast.LENGTH_LONG).show();
}
}

【讨论】:

  • 拯救我的一天!谢谢朋友。
  • 很高兴我能帮上忙! :)
猜你喜欢
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2017-02-22
  • 2017-10-16
  • 1970-01-01
相关资源
最近更新 更多