在另一个类中设置齐射JSON对象参数

时间:2018-06-24 04:58:59

标签: android android-volley

我有墙纸应用程序。应用使用排球发送了请求。

 public void getJsonResponsePost() {

    pDialog.setMessage("Downloading json...");
    pDialog.show();
    Map<String, String> params = new HashMap();
    params.put("table", "animal");
    JSONObject json = new JSONObject(params);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, script, json,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    // Do Something with response.

                    adapter.notifyDataSetChanged();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    jsonObjectRequest.setTag(REQ_TAG);
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance().addToRequestQueue(jsonObjectRequest);

}

服务器获取表输入,然后从数据库发送响应。 现在,我希望该用户可以过滤图像。我有一个自定义对话框,用户可以在其中选择单选按钮。当用户选择一张图片并提交时,我希望排球请求的参数应该像这样改变。

params.put("table", "animal");
// to
params.put("table", "nature");

自定义对话框代码

submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int radioButtonID = radioGroup.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) 
            radioGroup.findViewById(radioButtonID);
            Toast.makeText(getActivity(),radioButton.getText(), 
            Toast.LENGTH_SHORT).show();
       // Set param for volley here from radio button value.
        }
    });

1 个答案:

答案 0 :(得分:1)

您可以使用-

int id = radioGroup.getCheckedRadioButtonId();
String text = "";
Map<String, String> params = new HashMap();
if(id == your_first_radio_button_id) {
    // First radio button is selected.
    text = firstRB.getText().toString();
} 
if(id == your_second_radio_button_id) {
    // Second radio button is selected.
    text = secondRB.getText().toString();
}
if(!text.equals("")) {
     params.put("table", "nature");
}
else {
    params.put("table", "animal");
}