onResume()方法中的Volley或HttpURLConnection无法正常工作

时间:2016-10-28 06:47:11

标签: android android-volley httpurlconnection onresume

嗨我需要在应用程序进入后台并回到前台时调用Web服务。我尝试了Volley和HttpURLConnection但不幸的是它们都不适合我。有时它会给出结果但有时却不是.Below是我的代码

@Override
protected void onResume() {
    try {
        super.onResume();
       // app.activityResumed();
       // Toast.makeText(this, "QuizActivity Resumed", Toast.LENGTH_LONG).show();

       /* if (session.getAreaName()!= null) {
            if (GetGeoCodingTask.mAutoCompText != null)
                autoCompView.setText(GetGeoCodingTask.mAutoCompText);
        }*/
        if (GetGeoCodingTask.mAutoCompText != null) {
            autoCompView.setText(GetGeoCodingTask.mAutoCompText);
        }

        autoCompView.post(new Runnable() {
            public void run() {
                autoCompView.dismissDropDown();
            }
        });

        LoadFoodListData();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }

}

我使用HttpURLConnection的方法是

private void LoadFoodListData() {
    try {
        StringBuffer chaine = new StringBuffer("");
        URL url = new URL(myUrl);
        connectionOpp = (HttpURLConnection) url.openConnection();
        //connection.setReadTimeout(15000);
        //connection.setConnectTimeout(15000);
        connectionOpp.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        connectionOpp.setRequestMethod("POST");
        connectionOpp.setDoInput(true);
        connectionOpp.setDoOutput(true);

        Log.d("Tag", "Webservice after connect");

        OutputStream os = connectionOpp.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(child.toString());

        writer.flush();
        writer.close();
        os.close();
        connectionOpp.connect();

        int responseCode = connectionOpp.getResponseCode();
        Log.d("Tag", String.valueOf(responseCode));
        if (responseCode == HttpsURLConnection.HTTP_OK) {
            // Toast.makeText(mContext, "Httpok",Toast.LENGTH_SHORT).show();
            InputStream inputStream = connectionOpp.getInputStream();
            Log.d("Tag", "Webservice httpok");
            BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while ((line = rd.readLine()) != null) {
                chaine.append(line);
            }
        }
        String readJSON = chaine.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

和使用Volley是

private void LoadFoodListData() {   

    final ProgressDialog ringProgressDialog = ProgressDialog.show(QuizActivity.this, "Please wait ...", "Fetching data nearby you ...", true);

    ringProgressDialog.setCancelable(false);
    JsonObjectRequest jsObjRequest = null;
    try {
        jsObjRequest = new JsonObjectRequest(Request.Method.POST, myurl, child,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            ongoing = response.getJSONArray("Listongoing");
                            if (ongoing != null) {
                                if (ongoing.length() != 0) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            livecount.setText("(" + ongoing.length() + ")");
                                        }
                                    });
                                }
                            }

                            upcoming = response.getJSONArray("ListUpcoming");
                            Expire = response.getJSONArray("ListExpire");
                            if (Expire != null) {
                                if (Expire.length() != 0) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            expiredcount.setText("(" + Expire.length() + ")");
                                        }
                                    });
                                }
                            }
                        }
                        catch (JSONException e) {
                            e.printStackTrace();
                            ringProgressDialog.dismiss();
                        }
                        // dialogPro.cancel();
                        ringProgressDialog.dismiss();
                    }
                }, 
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        ringProgressDialog.dismiss();
                   }
                });
    }   catch (Exception e) {
            ringProgressDialog.dismiss();
        }
        if (jsObjRequest != null) {
            jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        }
        AppController.getInstance().addToRequestQueue(jsObjRequest);
}

任何帮助都会非常感激。

0 个答案:

没有答案