Android application crashing on 3G, slow connection, but works over WIFI

删除回忆录丶 提交于 2019-12-11 14:44:28

问题


Im optimizing a simple android app that uses HTTP GET calls (with Apache HttpClient) to read data from a web server. The data that is transferred is in JSON format.

The application is pretty slow while on WIFI, but everytime the phone switches to 3G the app is getting the URL but it's failing to parse.

org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

Im using AsyncTask to switch on a background thread:

    private class DownloadJSONRepertoire extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    protected Void doInBackground(Void... params) {
        arrayList = new ArrayList<HashMap<String, String>>();
        jsonObject = JSONfunctions.getJSONfromURL("http://example");
        try {

            jsonArray = jsonObject.getJSONArray("posts");
            for(int i = 0; i< jsonArray.length();i++)
            {
                HashMap<String,String> map = new HashMap<String,String>();
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String url = jsonObject1.getString("url");
                String title = jsonObject1.getString("title");
                String content = jsonObject1.getString("content");
                map.put(SHAREURL,url);
                map.put(TITLE,title);
                map.put(CONTENT,content);
                JSONArray jsonArray1 = jsonObject1.getJSONArray("attachments");
                for(int j=0;j<jsonArray1.length();j++){
                    JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                    String urlImage = jsonObject2.getString("url");
                    map.put(URL, urlImage);
                    arrayList.add(map);

                }
            }
        }
        catch (JSONException e){
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void args){
        listView = (ListView) mActivity.findViewById(android.R.id.list);
        adapter = new ListViewAdapter(mActivity, arrayList);
        setListAdapter(adapter);
        adapter.notifyDataSetChanged();
        progressBar.setVisibility(View.INVISIBLE);
    }
}

JSONfunctions class:

public class JSONfunctions {
    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

    // Download JSON data from URL
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        HttpResponse response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {

        Log.e("log_tag", "Failed to connect" + e.toString());
    }

    // Convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    try {

        jArray = new JSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
}
}

回答1:


you are getting too much data in response so there might be possibility that in slow Internet connection connection get close
Either use better Volley or Retrofit

You can provide your own timeout and provide success and failure callback for network call



来源:https://stackoverflow.com/questions/29987024/android-application-crashing-on-3g-slow-connection-but-works-over-wifi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!