AsyncTask中的java错误导致随机响应
我正在使用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(安卓.R.id.content), "Invalid Credentials", Snackbar.LENGTH_LONG).show();
}
/*
}
catch (Exception e){
Snackbar.make(this.findViewById(安卓.R.id.content), "Check data connection", Snackbar.LENGTH_LONG).show();
e.printStackTrace();
}*/
}
我故意没有捕捉到异常,并通过Crashlytics追踪到错误。错误发生在这一行
JsonArray jArray = parser.parse(jsonString).getAsJsonArray();
上面写着:
Fatal Exception: java.lang.IllegalStateException: This is not a JSON Array.
我认为当响应为空时也会发生这种情况,而且由于我无法在服务器中跟踪请求,我认为这是因为没有发送任何请求。(如果错了,请纠正我)
所以,我的问题是:我是否遗漏了什么|或者我定义任务的方式是否有任何错误
编辑1
PS: I am using an HTTPS URL in this AsyncTask
# 1 楼答案
在Crashalitics中,您可以记录调试消息,并从捕获的异常中堆栈跟踪。因此,你的答案是适当地收集这些信息,然后问题就会清楚了
在可能发生的崩溃之前,还要记录日志消息