如何建立可靠的网络会话?

时间:2013-11-25 04:25:29

标签: android

偶尔,我可以看到我的应用不会从网站下载数据。同时我可以使用相同的api和相同的网络在PC上访问相同的数据。因此,我想知道如何使会话更可靠。目前我正在使用以下内容:
AsyncTask,用于执行网络操作。

public class SearchAdd extends AsyncTask<Void, Void, JSONObject> {

    private SearchAddInterface callback;

    public SearchAdd(SearchAddInterface callback) {
        this.callback = callback;
    }

    @Override
    protected JSONObject doInBackground(Void... arg0) {
        return JSONParser.getJSONfromURL(RequestStringCreater
                .concatenationStrings().replaceAll("\\s", "%20"));
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
        ArrayList<Integer> ids = new ArrayList<Integer>();
        int premium = 0;
        int up = 0;
        String not_photo;
        int pageCount = 1;

        try {

            if (result != null && result.has("count")) {

                myList = parse(result);

                }}
                callback.onSearchAdd(myList);
                callback.returnIds(ids);
            } else {
                callback.returnIds(null);
                callback.onSearchAdd(null);
            }
        } catch (JSONException e) {
            callback.returnIds(null);
            callback.onSearchAdd(null);
            Log.e("log_tag", "Error parsing data" + e.toString());

        }
        // Done! now continue on the UI thread
    }

JSONParser - 发送请求并接收请求。

public static JSONObject getJSONfromURL(String url) {

        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        if (isOnline()) {
            // http post
            try {
                HttpClient httpclient = UILApplication.getHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.d("URL", url);
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
                return null;
            }
            // convert response to string
            StringBuilder sb = new StringBuilder();
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                Log.d("RESPONSE", result);

            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString()+"JSON: "+sb);
                return null;
            }
            try {
                Log.d("RESULT: ", result);
                jArray = new JSONObject(result);
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString()+"JSON: "+sb);
                return null;
            }
            if (result.contains("error\":2")) {
                Log.e("JSON", jArray.toString());
                UILApplication.login = 2;
                return null;
            }
            if (jArray != null) {
                Log.d("JSON", jArray.toString());
            } 
            return jArray;
        } // else promtError();
            // error();
        return null;
    }

0 个答案:

没有答案