Android应用程序崩溃的3G,连接速度慢,但工程通过WiFi [英] Android application crashing on 3G, slow connection, but works over WIFI

查看:120
本文介绍了Android应用程序崩溃的3G,连接速度慢,但工程通过WiFi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林优化使用HTTP GET调用(与Apache的HttpClient)从Web服务器读取数据的简单Android应用程序。
所传输的数据是JSON格式。

该应用程序是在WIFI pretty虽然缓慢,但每次电话切换到3G的应用是获得URL,但它的失败来解析。

  org.json.JSONException:值< java.lang.String类型的DOCTYPE不能转换为JSONObject的!

即时通讯使用的AsyncTask在后台线程切换:

 私有类DownloadJSONRepertoire扩展的AsyncTask<太虚,太虚,太虚> {
    @覆盖
    在preExecute保护无效(){
        progressBar.setVisibility(View.VISIBLE);
    }
    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        ArrayList的=新的ArrayList<&HashMap的LT;字符串,字符串>>();
        的JSONObject = JSONfunctions.getJSONfromURL(HTTP://例);
        尝试{            jsonArray = jsonObject.getJSONArray(上岗);
            的for(int i = 0; I< jsonArray.length();我++)
            {
                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                JSONObject的jsonObject1 = jsonArray.getJSONObject(I)
                字符串URL = jsonObject1.getString(URL);
                字符串title = jsonObject1.getString(标题);
                字符串内容= jsonObject1.getString(内容);
                map.put(SHAREURL,URL);
                map.put(标题,标题);
                map.put(内容,内容);
                JSONArray jsonArray1 = jsonObject1.getJSONArray(附件);
                对于(INT J = 0; J< jsonArray1.length(); J ++){
                    JSONObject的jsonObject2 = jsonArray1.getJSONObject(J);
                    字符串urlImage = jsonObject2.getString(URL);
                    map.put(URL,urlImage);
                    arrayList.add(地图);                }
            }
        }
        赶上(JSONException E){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
    }
    @覆盖
    保护无效onPostExecute(无效参数){
        ListView控件=(ListView控件)mActivity.findViewById(android.R.id.list);
        适配器=新ListViewAdapter(mActivity,ArrayList的);
        setListAdapter(适配器);
        adapter.notifyDataSetChanged();
        progressBar.setVisibility(View.INVISIBLE);
    }
}

JSONfunctions类:

 公共类JSONfunctions {
    公共静态的JSONObject getJSONfromURL(字符串URL){
        InputStream为= NULL;
        字符串结果=;
        JSONObject的jArray = NULL;    // URL从下载JSON数据
    尝试{
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(URL);        HTT presponse响应= httpclient.execute(httppost);        HttpEntity实体= response.getEntity();
        是= entity.getContent();    }赶上(例外五){        Log.e(log_tag,无法连接+ e.toString());
    }    //响应转换成字符串
    尝试{
        读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+\\ n);
        }
        is.close();
        结果= sb.toString();
    }赶上(例外五){
        Log.e(log_tag,错误转换结果+ e.toString());
    }    尝试{        jArray =新的JSONObject(结果);
    }赶上(JSONException E){
        Log.e(log_tag,错误分析数据+ e.toString());
    }    返回jArray;
}
}


解决方案

你在响应得到太多数据,因此有可能是可能性,在缓慢​​的互联网连接的连接亲近结果
无论是用更好的凌​​空或的改造

您可以提供自己的超时,并提供网络呼叫成功和失败的回调

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

解决方案

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

这篇关于Android应用程序崩溃的3G,连接速度慢,但工程通过WiFi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆