Android应用程序在第二次AsyncTask调用时崩溃了吗?

时间:2015-10-29 11:13:56

标签: android android-asynctask

我在棒棒糖上运行应用程序,所以根据一些专业人士的说法,默认的asynctask运行是序列化的。该应用程序将首先验证Instagram用户,然后向Instagram询问用户的一些内容。第一步是第一步AsyncTask调用工作正常,但第二步调用使应用程序变为现实! LogCat按摩是:java.lang.RunTimeException,执行doInBackground时发生错误。

public class MainActivity extends Activity {
private WebView webView;
private String code;
private String myToken;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String myUrl  = "https://api.instagram.com/oauth/authorize/?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=code";
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView wView, String url) {
               if (url.indexOf(REDIRECT_URI) > -1) {
                   code = url.substring(28, url.length());
                   runAsync();                    
                   return true;
               } else
                   return false; 
               }
       });
    webView.loadUrl(myUrl);
}

private void runAsync(){
    //first call which works fine and gains access_token
    new MyHttpsPost().execute(
           new String ("https://api.instagram.com/oauth/access_token"),
           new String ("client_id="),
           new String ("&client_secret="),
           new String ("&grant_type="),
           new String ("&redirect_uri="),
           new String ("&code="),
           new String (CLIENT_ID),
           new String (CLIENT_SECRET),
           new String ("authorization_code"),
           new String (REDIRECT_URI),
           new String (code),

           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String ("access_token"),
           new String (""),
           new String (""),
           new String (""),
           new String ("")
           );
    //second call which makes the app crash, "error while executing doInBackground"
    new MyHttpsPost().execute(
           new String ("https://api.instagram.com/v1/media/popular?access_token=" + myToken),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),

           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String (""),
           new String ("data"),
           new String (""),
           new String (""),
           new String (""),
           new String ("")
           );
}

class MyHttpsPost extends AsyncTask<String,String,String>{
protected String doInBackground(String... params) {

   URL murl;
   HttpsURLConnection conn;
   String response= "";

   try{
   murl=new URL(params[0]);

   String param=
   params[1]     +URLEncoder.encode(params[6],"UTF-8")+
   params[2]+URLEncoder.encode(params[7],"UTF-8")+
   params[3]   +URLEncoder.encode(params[8],"UTF-8")+
   params[4] +URLEncoder.encode(params[9],"UTF-8")+
   params[5]         +URLEncoder.encode(params[10],"UTF-8");

   conn=(HttpsURLConnection)murl.openConnection();
   conn.setDoOutput(true);
   conn.setRequestMethod("POST");
   conn.setFixedLengthStreamingMode(param.getBytes().length);
   conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

   PrintWriter out = new PrintWriter(conn.getOutputStream()); 
   out.print(param);
   out.close(); 

   Scanner inStream = new Scanner(conn.getInputStream());
   while(inStream.hasNextLine())
   response+=(inStream.nextLine());

   }
   catch(MalformedURLException ex){
   Toast.makeText(MainActivity.this, ex.toString(), Toast.LENGTH_SHORT ).show();
   }
   catch(IOException ex){
   Toast.makeText(MainActivity.this, ex.toString(), Toast.LENGTH_SHORT ).show();
   }

  try {
        JSONObject jsonObj = (JSONObject) new      JSONTokener(response).nextValue();
        params[11] = jsonObj.getString(params[16]);
        params[12] = jsonObj.getString(params[17]);         
        params[13] = jsonObj.getString(params[18]);
        params[14] = jsonObj.getString(params[19]);
        params[15] = jsonObj.getString(params[20]);

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

    myToken = params[11];
    return params[11];      
}    
protected void onPostExecute(String result) {
    Toast.makeText(getBaseContext(), result , Toast.LENGTH_LONG ).show();
    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setText(result);    
}    
}  

这是LogCat:

      10-29 15:10:02.989: D/ResourcesManager(20355): creating new AssetManager and set to /data/app/com.example.webview-2/base.apk
      10-29 15:10:03.044: V/BitmapFactory(20355): DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/ic_ab_back_holo_dark_am.png
      10-29 15:10:03.064: V/BitmapFactory(20355): DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/sym_def_app_icon.png
      10-29 15:10:03.134: I/WebViewFactory(20355): Loading com.google.android.webview version 42.0.2311.129 (code 2311129)
      10-29 15:10:03.134: D/ResourcesManager(20355): creating new AssetManager and set to /system/app/WebViewGoogle/WebViewGoogle.apk
      10-29 15:10:03.164: I/LibraryLoader(20355): Time to load native libraries: 2 ms (timestamps 4641-4643)
      10-29 15:10:03.164: I/LibraryLoader(20355): Expected native library version number "",actual native library version number ""
      10-29 15:10:03.214: V/WebViewChromiumFactoryProvider(20355): Binding Chromium to main looper Looper (main, tid 1) {18bd9fce}
      10-29 15:10:03.214: I/LibraryLoader(20355): Expected native library version number "",actual native library version number ""
      10-29 15:10:03.214: I/chromium(20355): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
      10-29 15:10:03.239: I/BrowserStartupController(20355): Initializing chromium process, singleProcess=true
      10-29 15:10:03.244: W/art(20355): Attempt to remove local handle scope entry from IRT, ignoring
      10-29 15:10:03.244: E/SysUtils(20355): ApplicationContext is null in ApplicationStatus
      10-29 15:10:03.269: W/AudioManagerAndroid(20355): Requires BLUETOOTH permission
      10-29 15:10:03.269: W/chromium(20355): [WARNING:resource_bundle.cc(286)] locale_file_path.empty()
      10-29 15:10:03.269: I/chromium(20355): [INFO:aw_browser_main_parts.cc(63)] Load from apk succesful, fd=42 off=50364 len=3345
      10-29 15:10:03.269: I/chromium(20355): [INFO:aw_browser_main_parts.cc(76)] Loading webviewchromium.pak from, fd:43 off:7953004 len:1161174
      10-29 15:10:03.274: I/(20355): PLATFORM VERSION : JB-MR-2
      10-29 15:10:03.414: W/chromium(20355): [WARNING:data_reduction_proxy_config.cc(150)] SPDY proxy OFF at startup
      10-29 15:10:03.439: W/art(20355): Attempt to remove local handle scope entry from IRT, ignoring
      10-29 15:10:03.459: W/AwContents(20355): onDetachedFromWindow called when already detached. Ignoring
      10-29 15:10:03.474: V/BitmapFactory(20355): DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi/ic_launcher.png
      10-29 15:10:03.514: D/Activity(20355): performCreate Call secproduct feature valuefalse
      10-29 15:10:03.514: D/Activity(20355): performCreate Call debug elastic valuetrue
      10-29 15:10:03.534: D/OpenGLRenderer(20355): Render dirty regions requested: true
      10-29 15:10:03.539: D/Atlas(20355): Validating map...
      10-29 15:10:03.594: I/OpenGLRenderer(20355): Initialized EGL, version 1.4
      10-29 15:10:03.604: I/OpenGLRenderer(20355): HWUI protection enabled for context ,  &this =0x9f5c0060 ,&mEglDisplay = 1 , &mEglConfig = -1282169932 
      10-29 15:10:03.614: D/OpenGLRenderer(20355): Enabling debug mode 0
      10-29 15:10:03.769: I/Timeline(20355): Timeline: Activity_idle id: android.os.BinderProxy@2d6ba73d time:232065247
      10-29 15:10:08.609: W/BindingManager(20355): Cannot call determinedVisibility() - never saw a connection for the pid: 20355
      10-29 15:10:08.684: I/System.out(20355): (HTTPLog)-Static: isSBSettingEnabled false
      10-29 15:10:08.684: I/System.out(20355): (HTTPLog)-Static: isShipBuild true
      10-29 15:10:08.684: I/System.out(20355): (HTTPLog)-Thread-50224-192410000: SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
      10-29 15:10:08.684: I/System.out(20355): (HTTPLog)-Static: isSBSettingEnabled false
      10-29 15:10:08.964: I/System.out(20355): KnoxVpnUidStorageknoxVpnSupported API value returned is false
      10-29 15:10:10.109: W/System.err(20355): org.json.JSONException: No value for 
      10-29 15:10:10.114: W/System.err(20355):  at org.json.JSONObject.get(JSONObject.java:389)
      10-29 15:10:10.114: W/System.err(20355):  at org.json.JSONObject.getString(JSONObject.java:550)
      10-29 15:10:10.119: W/System.err(20355):  at com.example.webview.MainActivity$MyHttpsPost.doInBackground(MainActivity.java:172)
      10-29 15:10:10.119: W/System.err(20355):  at com.example.webview.MainActivity$MyHttpsPost.doInBackground(MainActivity.java:1)
      10-29 15:10:10.119: W/System.err(20355):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
      10-29 15:10:10.119: W/System.err(20355):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      10-29 15:10:10.119: W/System.err(20355):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
      10-29 15:10:10.119: W/System.err(20355):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      10-29 15:10:10.119: W/System.err(20355):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      10-29 15:10:10.119: W/System.err(20355):  at java.lang.Thread.run(Thread.java:818)
      10-29 15:10:10.129: I/System.out(20355): (HTTPLog)-Static: isSBSettingEnabled false
      10-29 15:10:10.419: E/AndroidRuntime(20355): FATAL EXCEPTION: AsyncTask #3
      10-29 15:10:10.419: E/AndroidRuntime(20355): Process: com.example.webview, PID: 20355
      10-29 15:10:10.419: E/AndroidRuntime(20355): java.lang.RuntimeException: An error occured while executing doInBackground()
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.os.AsyncTask$3.done(AsyncTask.java:300)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.lang.Thread.run(Thread.java:818)
      10-29 15:10:10.419: E/AndroidRuntime(20355): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.os.Handler.<init>(Handler.java:200)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.os.Handler.<init>(Handler.java:114)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.widget.Toast$TN.<init>(Toast.java:487)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.widget.Toast.<init>(Toast.java:121)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.widget.Toast.makeText(Toast.java:290)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at com.example.webview.MainActivity$MyHttpsPost.doInBackground(MainActivity.java:163)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at com.example.webview.MainActivity$MyHttpsPost.doInBackground(MainActivity.java:1)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      10-29 15:10:10.419: E/AndroidRuntime(20355):  ... 4 more

2 个答案:

答案 0 :(得分:1)

您的异常表示您正在尝试从doInBackground()更新UI,您不应该这样做。

查看您的代码,您唯一能做的就是:

catch(MalformedURLException ex){
      Toast.makeText(MainActivity.this, ex.toString(), Toast.LENGTH_SHORT ).show();
 }
catch(IOException ex){
      Toast.makeText(MainActivity.this, ex.toString(), Toast.LENGTH_SHORT ).show();
 }

所以你的第一个问题就是你得到了一个你捕获的异常,然后你尝试Toast it,并得到一个未处理的异常。

所以删除这个Toast,但是首先要检查你为什么会得到一个例外。

答案 1 :(得分:0)

问题不在Asynctask中,而在于您的JSON或序列化它的方式。 logcat的这一行显示:

10-29 15:10:10.109:W / System.err(20355):org.json.JSONException:没有值   10-29 15:10:10.114:W / System.err(20355):at org.json.JSONObject.get(JSONObject.java:389)

检查您从Web服务获得的响应,并检查您反序列化JSON的方式是否正确。

我最好的猜测是你的params数组大部分为空都会导致错误。你基本上在做json.getString(""),你的JSON是用空键填充的吗?

修改

Udi Idan是对的,这次崩溃的直接原因是在UI线程上调用Toast.show()(你做不到),但也要注意应用程序在此之前捕获JSONException,所以你会最终也遇到了麻烦。

为了在你在AsyncTask中工作时在UI线程上执行某些操作是在onPostExecute(result)方法上执行它,这是在主(ui)线程上执行的。

另一种方法是创建一个runnable并在UI线程上执行它,如下所示:

Runnable toastRunnable = new Runnable()
{
    public void run()
    {
        Toast.makeText(MainActivity.this, "My Toast message", Toast.LENGTH_SHORT)
                    .show();
    }
};

context.runOnUiThread(toastRunnable);