SwipeRefreshLayout会永久加载,并且无法完成其发送请求的任务

时间:2018-11-01 17:26:12

标签: java android http

我编写了一个非常简单的代码,该代码假定当用户刷新页面时下载json时,我在其中编写的所有内容都在其他地方起作用,而在setOnRefreshListener中却没有。

以下是代码:

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {


            Log.d("serviceAs2", "onHandleIntent secondservice called");
            SharedPreferences logInDataSP =getSharedPreferences("LocalLogInData", 0);
            SharedPreferences sp=getSharedPreferences("JsonData", 0);
            SharedPreferences.Editor editor = sp.edit();
            try {



                Log.d("serviceAs22", "onHandleIntent secondservice called");
                Log.d("serviceAs22", toLoad);

                HttpHandler sh = new HttpHandler();
                String jsonStr ="a";

                if(toLoad == "kinderGarden"){
                    jsonStr = sh.makeServiceCall(getString(R.string.UrlKinderGarden));
                    editor.putString("JsonKG", jsonStr);}

                if(toLoad == "fourthGrade") {
                jsonStr = sh.makeServiceCall(getString(R.string.UrlFourthGrade));
                editor.putString("JsonFG", jsonStr);}

                if(toLoad == "seventhGrade"){
                    Log.d("serviceAs22", toLoad + "sfsf");
                jsonStr = sh.makeServiceCall(getString(R.string.UrlSeventhGrade));
                editor.putString("JsonSG", jsonStr);}

                if(toLoad == "tenthGrade"){
                jsonStr = sh.makeServiceCall(getString(R.string.UrlTenthGrade));
                editor.putString("JsonTG", jsonStr);}

                if(toLoad == "gameLibrary") {
                jsonStr = sh.makeServiceCall(getString(R.string.UrlGamesLibrary));
                editor.putString("JsonGL", jsonStr);}

                if (toLoad == "summerCamps") {
                    jsonStr = sh.makeServiceCall(getString(R.string.UrlSummerCamps));
                    editor.putString("JsonSC", jsonStr);
                }

                if(toLoad == "trompim"){
                jsonStr = sh.makeServiceCall(getString(R.string.UrlTrompim));
                editor.putString("JsonT", jsonStr);}

                if(toLoad == "pinukim"){
                jsonStr = sh.makeServiceCall(getString(R.string.UrlPinukim));
                editor.putString("JsonP", jsonStr);}

                editor.commit();

                Log.d("Nugi2626", jsonStr);
            } catch (Exception e) {
                // TODO: handle exception
            }
            swipeRefreshLayout.destroyDrawingCache();;

        }
    });

这是HttpHandler:

public class HttpHandler {

private static final String TAG = HttpHandler.class.getSimpleName();

public HttpHandler() {
}

public String makeServiceCall(String reqUrl) {
    String response = null;
    try {
        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
}

我想不出任何会导致它无法正常运行的东西,因为我在intentService中使用了完全相同的操作,并且可以正常工作十亿次。
你们知道发生了什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

谢谢大家,解决方案的确是用Thread包装它:

Thread thread = new Thread() {
                String jsonStr ="a";
                @Override
                public void run() {

                    Log.d("serviceAs2", "onHandleIntent secondservice called");
                    SharedPreferences.Editor editor = sp.edit();
                    arrayListMain=null;
                    finalLvPos=10;

                    try {



                        Log.d("serviceAs22", "onHandleIntent secondservice called");
                        Log.d("serviceAs22", toLoad);

                        sh = new HttpHandler();

                        if(toLoad == "kinderGarden"){
                            Log.d("serviceAs22", toLoad + "sfsf");
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlKinderGarden));
                            editor.putString("JsonKG", jsonStr);
                        }

                        if(toLoad.matches("fourthGrade")) {
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlFourthGrade));
                            editor.putString("JsonFG", jsonStr);
                        }

                        if(toLoad == "seventhGrade"){
                            Log.d("serviceAs22", toLoad + "sfsf");
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlSeventhGrade));
                            editor.putString("JsonSG", jsonStr);}

                        if(toLoad == "tenthGrade")
                        {
                            Log.d("serviceAs22", toLoad + "sfsf");
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlTenthGrade));
                            editor.putString("JsonTG", jsonStr);
                        }

                        if(toLoad == "gameLibrary") {
                            Log.d("serviceAs22", toLoad + "sfsf");
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlGamesLibrary));
                            editor.putString("JsonGL", jsonStr);
                            Log.d("serviceAs22", toLoad + "sfsf");}

                        if (toLoad == "summerCamps") {
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlSummerCamps));
                            editor.putString("JsonSC", jsonStr);
                        }

                        if(toLoad == "trompim"){
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlTrompim));
                            editor.putString("JsonT", jsonStr);}

                        if(toLoad == "pinukim"){
                            jsonStr = sh.makeServiceCall(getString(R.string.UrlPinukim));
                            editor.putString("JsonP", jsonStr);}
                        editor.commit();


                        Log.d("Nugi2626", jsonStr);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }

}; 并在之后立即启动线程。