如何在Android中使用Volley Library获得响应?

时间:2016-06-27 07:10:08

标签: android response android-volley

我正在使用Volley Library来获取服务器的响应。 响应成功提前,但现在突然没有得到响应。

我不知道,我的代码中有什么问题?

我的代码是,

  public static void makeJsonData(final Activity activity, final String topic_id, final String user_id) {

        context = activity;
        dbAdapter = new DBAdapter(context);

        pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);

        showpDialog();

        JSONObject params = new JSONObject();
        try {
            params.put("topic_id", topic_id);
            params.put("user_id", user_id);
            params.put("action", "getquestions");

            System.out.println("!!!!!!!!!params======"+params);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                Url.URL, params, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                try {
                    // Parsing json object response
                    // response will be a json object
                    Boolean resultFlag = response.getBoolean("resultFlag");

                    if (resultFlag == true) {
                        String success = response.getString("successMessage");

                        JSONArray json_array_question = response.getJSONArray("Questions");

                        for (int i = 0; i < json_array_question.length(); i++) {

                            JSONObject json_object_question = json_array_question.getJSONObject(i);

                            quiz_id = json_object_question.getInt("QuizID");
                            question_type = json_object_question.getString("questionType");
                            time_required = json_object_question.getString("timeRequired");

                        }

                    } else if (resultFlag == false) {
                        String error = response.getString("errorMessage");
                        Toast.makeText(activity.getApplicationContext(), error, Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(activity,
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(activity,
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }


    private static void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private static void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

响应是,

{
  "resultFlag": true,
  "successMessage": "Data Received",
  "Questions": [
    {
      "QuizID": 958,
      "questionType": "Trivia",
      "timeRequired": "10",
    }
    ]
}

请建议我。

感谢。

1 个答案:

答案 0 :(得分:1)

def grailsApplication

def init = { servletContext ->
    def applicationContext = servletContext.getAttribute(ApplicationAttributes.APPLICATION_CONTEXT)
    applicationContext.eventTriggeringInterceptor.datastores.each { k, datastore ->
    GormEventListener listener = new GormEventListener(datastore)
    listener.with{
        application = grailsApplication
    }
    applicationContext.addApplicationListener(listener)
}