嗨,我是android的初学者,所以我希望你能帮我详细说明
我使用AsyncTask建立了与MySQL数据库的连接,它很好地连接好我从数据库中获取数据但事情就是当我关闭我的数据库(数据库在wamp上)时我的应用程序强制关闭并且它给出了这个错误:java.util.concurrent.timeoutexception 我将把AsyncTask代码放在
之下private void get_banners(final int pages) {
AsyncHttpPost post = new AsyncHttpPost("http://192.168.1.102/soton/new.php");
post.setTimeout(5000);
MultipartFormDataBody body = new MultipartFormDataBody();
body.addStringPart("City", MainActivity.sp.getString("City", ""));
body.addStringPart("Page", String.valueOf(pages));
body.addStringPart("Cate", "all");
post.setBody(body);
AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
@Override
public void onCompleted(final Exception e, AsyncHttpResponse source, final String result) {
if (e != null) {
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.activity, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
if (!result.equals("")) {
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
//you can toast the result here
//Toast.makeText(MainActivity.activity, result,Toast.LENGTH_LONG).show();
if (page == 0) {
hash_all.clear();
}
items.clone();
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
HashMap<String, Object> hash_add = new HashMap<>();
hash_add.put("ID", object.getString("ID"));
hash_add.put("Username", object.getString("Username"));
hash_add.put("Title", object.getString("Title"));
hash_add.put("Description", object.getString("Description"));
hash_add.put("Price", object.getString("Price"));
hash_add.put("Tell", object.getString("Tell"));
hash_add.put("Email", object.getString("Email"));
hash_add.put("City", object.getString("City"));
hash_add.put("Cate", object.getString("Cate"));
hash_add.put("Img1", object.getString("Img1"));
hash_add.put("Img2", object.getString("Img2"));
hash_add.put("Img3", object.getString("Img3"));
hash_add.put("Date", object.getString("Date"));
hash_all.add(hash_add);
items = new String[hash_all.size()];
}
ad.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
else {
Toast.makeText(MainActivity.activity,result,Toast.LENGTH_LONG).show();
}
}
});
}
更新:堆栈跟踪
FATAL EXCEPTION: AsyncServer
Process: com.morteza.newproject, PID: 3211
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.morteza.newproject.Frag_banners_all$3.onCompleted(Frag_banners_all.java:145)
at com.morteza.newproject.Frag_banners_all$3.onCompleted(Frag_banners_all.java:130)
at com.koushikdutta.async.http.AsyncHttpClient.invokeWithAffinity(AsyncHttpClient.java:527)
at com.koushikdutta.async.http.AsyncHttpClient.access$800(AsyncHttpClient.java:51)
at com.koushikdutta.async.http.AsyncHttpClient$7.run(AsyncHttpClient.java:534)
at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:740)
at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:758)
at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:658)
at com.koushikdutta.async.AsyncServer.access$800(AsyncServer.java:44)
at com.koushikdutta.async.AsyncServer$14.run(AsyncServer.java:600)
W / System.err:java.util.concurrent.TimeoutException W / System.err:at com.koushikdutta.async.http.AsyncHttpClient $ 2.run(AsyncHttpClient.java:246) W / System.err:at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:740) W / System.err:at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:758) W / System.err:at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:658) W / System.err:at com.koushikdutta.async.AsyncServer.access $ 800(AsyncServer.java:44) W / System.err:at com.koushikdutta.async.AsyncServer $ 14.run(AsyncServer.java:600) 申请已终止。
答案 0 :(得分:0)
问题soved我通过在代码中添加了一个else来解决它,当前代码在
之下private void get_banners(final int pages) {
AsyncHttpPost post = new AsyncHttpPost("http://192.168.1.102/soton/new.php");
post.setTimeout(5000);
MultipartFormDataBody body = new MultipartFormDataBody();
body.addStringPart("City", MainActivity.sp.getString("City", ""));
body.addStringPart("Page", String.valueOf(pages));
body.addStringPart("Cate", "all");
post.setBody(body);
try {
AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
@Override
public void onCompleted(final Exception e, AsyncHttpResponse source, final String result) {
if (e != null) {
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.activity, "server is offline", Toast.LENGTH_LONG).show();
e.printStackTrace();
mSwipeRefreshLayout.setRefreshing(false);
}
});
}
//this is the answer
else {
if (!result.equals("")) {
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
//you can toast the result here
//Toast.makeText(MainActivity.activity, result,Toast.LENGTH_LONG).show();
if (page == 0) {
hash_all.clear();
}
items.clone();
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
HashMap<String, Object> hash_add = new HashMap<>();
hash_add.put("ID", object.getString("ID"));
hash_add.put("Username", object.getString("Username"));
hash_add.put("Title", object.getString("Title"));
hash_add.put("Description", object.getString("Description"));
hash_add.put("Price", object.getString("Price"));
hash_add.put("Tell", object.getString("Tell"));
hash_add.put("Email", object.getString("Email"));
hash_add.put("City", object.getString("City"));
hash_add.put("Cate", object.getString("Cate"));
hash_add.put("Img1", object.getString("Img1"));
hash_add.put("Img2", object.getString("Img2"));
hash_add.put("Img3", object.getString("Img3"));
hash_add.put("Date", object.getString("Date"));
hash_all.add(hash_add);
items = new String[hash_all.size()];
}
ad.notifyDataSetChanged();
mSwipeRefreshLayout.setRefreshing(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} else {
Toast.makeText(MainActivity.activity, result, Toast.LENGTH_LONG).show();
}
}
}
});
}catch (Exception e){
e.printStackTrace();
}
}