简体   繁体   English

Android应用程序在3G上崩溃,连接速度慢,但可通过WIFI正常运行

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

Im optimizing a simple android app that uses HTTP GET calls (with Apache HttpClient) to read data from a web server. 我正在优化一个简单的Android应用程序,该应用程序使用HTTP GET调用(与Apache HttpClient一起)从Web服务器读取数据。 The data that is transferred is in JSON format. 传输的数据为JSON格式。

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. 使用WIFI时,该应用程序的运行速度非常慢,但是每次手机切换到3G时,该应用程序都会获取URL,但无法解析。

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

Im using AsyncTask to switch on a background thread: 我正在使用AsyncTask打开后台线程:

    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: JSONfunctions类:

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;
}
}

you are getting too much data in response so there might be possibility that in slow Internet connection connection get close 您收到的数据过多,因此可能会在Internet连接缓慢的情况下关闭
Either use better Volley or Retrofit 使用更好的Volley翻新

You can provide your own timeout and provide success and failure callback for network call 您可以提供自己的超时时间,并提供网络呼叫的成功和失败回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android应用在3G慢速连接时崩溃,但可以在wifi上正常工作 - Android app crash on 3G slow connection but works fine over wifi Android应用程序可在3G上正常工作,但不能与WIFI一起使用 - Android application works fine in 3G but not with WIFI https连接通过Wifi(wlan)工作但不适用于3G / GPRS(umts) - https connection works over Wifi (wlan) but not for 3G/GPRS (umts) android应用程序在使用3g连接连接到webservice时挂起,并且可以正常使用WIFI - android application hangs when it connects to webservice using 3g connection and works fine with WIFI 超过3g连接的android mediaplayer无法正常工作,但可以在wifi上工作 - android mediaplayer over 3g connection not work but work on wifi Android应用程序不在3G网络中工作,但在WIFI中工作 - Android application not working in 3G network,but works in WIFI 在Android中检测连接是否为wifi,3G或EDGE? - Detect if connection is wifi, 3G or EDGE in android? 在Android中检查3G和W​​ifi的互联网连接 - Check internet connection with 3G and Wifi in Android Android 127.0.0.1(环回)连接:可在WiFi上运行,并因3G / 4G而失败 - Android 127.0.0.1 (loopback) connection: Works on WiFi, fails with 3G/4G Android应用程序未连接到Wifi,但在通过Wifi共享的3G中可以正常工作 - Android application not connecting to Wifi ,but works fine in 3G shared through Wifi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM