嗨,我是android的初学者,希望我能为您提供详细的帮助

我使用AsyncTask建立了与MySQL数据库的连接,这一切都很好,它可以很好地连接数据库,并且可以从数据库中获取数据,但是问题是当我关闭数据库(数据库处于关闭状态)时,我的应用程序强制关闭,并且出现此错误: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:位于com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:740)
W / System.err:位于com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:758)
W / System.err:位于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)
申请已终止。

最佳答案

问题解决了,我在代码中仅添加了一个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();
    }
}

07-24 09:37