AsyncTask中的错误导致随机响应

时间:2016-03-26 07:58:44

标签: java android android-asynctask

我正在使用AsyncTask进行网络通话。如果成功,它应该返回一个JSON数组,到目前为止它在我测试的设备上运行良好(Google nexus 5)。 但在摩托罗拉设备上,它不起作用。从某种意义上说,它甚至没有向服务器发送请求。

以下是代码:

private class SimpleTask extends AsyncTask<String, Void, String> {
    ProgressDialog dialog;

    @Override
    protected void onPreExecute()
    {
        dialog = new ProgressDialog(MainActivity.this,ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Loading Engine");
        dialog.show();
    }

    protected String doInBackground(String... urls)   {
        String result = "";
        try {

            //HttpGet httpGet = new HttpGet(urls[0]);
            HttpPost httpPost = new HttpPost(urls[0]);
            HttpClient client = new DefaultHttpClient();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("number",details));

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = client.execute(httpPost);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == 200) {
                InputStream inputStream = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader
                        (new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    result += line;
                }
            }

        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }
        //Log.w("PREMIERE::::",result);
        return result;
    }

    protected void onPostExecute(String jsonString)  {
        //dialog.dismiss();
        try {
            if ((this.dialog != null) && this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        } catch (final IllegalArgumentException e) {
            // Handle or log or ignore
        } catch (final Exception e) {
            // Handle or log or ignore
        } finally {
            this.dialog = null;
        }
        showData(jsonString);
    }
}

private void showData(String jsonString)
{
   // try
   // {
        Log.w("VALS:",""+jsonString);
        Gson gson = new Gson();
        JsonParser parser = new JsonParser();
        JsonArray jArray = parser.parse(jsonString).getAsJsonArray();
        SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
        for(JsonElement obj : jArray )
        {
            MainPojo cse = gson.fromJson( obj , MainPojo.class);
            uid.add(cse);

        }
        if(!uid.isEmpty())
        {
            new SimpleTask2().execute(URL2);
        }
        else
        {
            Snackbar.make(this.findViewById(android.R.id.content), "Invalid Credentials", Snackbar.LENGTH_LONG).show();
        }
   /*
    }
    catch (Exception e){
        Snackbar.make(this.findViewById(android.R.id.content), "Check data connection", Snackbar.LENGTH_LONG).show();
        e.printStackTrace();
    }*/

}

我有目的地,没有捕获异常,并通过Crashlytics追踪该错误。该行发生了这个错误。

JsonArray jArray = parser.parse(jsonString).getAsJsonArray();

说:

  

致命异常:java.lang.IllegalStateException:这不是JSON   阵列。

我认为当响应为空时也会发生这种情况,并且因为我无法在服务器中跟踪请求,我相信这是因为没有请求被发送。(如果错误则纠正我)。

所以,我的问题是:我错过了什么或者我定义AsyncTask的方式有什么问题吗?

编辑1

  

PS:我在这个AsyncTask中使用了一个HTTPS URL

1 个答案:

答案 0 :(得分:1)

在Crashalitics中,您可以记录调试消息并从捕获的异常中堆栈跟踪。所以答案就是正确收集这些信息,然后问题就会清楚

   // catch everythin n log everything
   } catch (ClientProtocolException e) {
       Crashlytics.logException(e);
    } catch (IOException e) {
       Crashlytics.logException(e);
    }

还会在可能的崩溃之前放置日志消息

Crashlytics.log("Here is the async task that keeps giving me headache");
相关问题