使用逗号分隔符volley android在for循环中发布字符串

时间:2017-04-21 06:53:08

标签: android android-volley

我有一个API,它接受带有逗号分隔符的String形式的参数,我需要使用android volley从arraylist中调用params。日志显示使用逗号分隔符正确发布它,只有循环的第一个元素正在获取发布。 我喜欢这个代码

     @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("user_id", String.valueOf(Common.UID));
        for(int m=0;m<Common.Category.size();m++)
        {
            if(Common.Category.size()>1) {
                if (m < Common.Category.size() - 1) {
                    Log.e("Posting Category params",Common.Category.get(m) + ",");
                    params.put("cat_id", Common.Category.get(m) + ",");
                } else {
                    Log.e("Posting cat last params", Common.Category.get(m));
                    params.put("cat_id", Common.Category.get(m));
                }
            }
            else
            {
                Log.e("only 1 Cat params", Common.Category.get(m));
                params.put("cat_id", Common.Category.get(m));
            }
        }
        for(int n=0;n<Common.langRemoveHash.size();n++) {
            if (Common.langRemoveHash.size() > 1) {
                {
                    if (n <Common.langRemoveHash.size() - 1) {
                        Log.e("Posting LangId params",Common.langRemoveHash.get(n) + ",");
                        params.put("lang_id", Common.langRemoveHash.get(n) + ",");
                    } else {
                        Log.e("Posting Last LangId ",Common.langRemoveHash.get(n));
                        params.put("lang_id", Common.langRemoveHash.get(n));
                    }
                }
            }
                else
                {
                    Log.e("Posting Only 1 Lang Id ",Common.langRemoveHash.get(n));
                    params.put("lang_id", Common.langRemoveHash.get(n));
                }

        }
        for(int p=0;p<subCatArraySize;p++)
        {
            if (p>1) {
                if (p<3) {
                    Log.e("Posting subCatId params", "5" + ",");
                    params.put("sub_cat_id", "5" + ",");
                } else {
                    Log.e("Posting last subcatId", "15");
                    params.put("sub_cat_id", "15");
                }
            }
            else
            {
                Log.e("Posting only 1 subcatId", "15");
                params.put("sub_cat_id", "15");
            }
        }
        //params.put("password","123");
        //int j=0;
        //for(String object: langId){
            //params.put("languageID["+(j++)+"]", object);
            // you first send both data with same param name as friendnr[] ....  now send with params friendnr[0],friendnr[1] ..and so on
        //}
        return params;
    }

};

LOGCAT显示正确的解析

04-21 12:04:12.943 23485-25015/
E/Posting Category params: 1,
04-21 12:04:12.943 23485-25015/        E/Posting cat last params: 3
04-21 12:04:12.943 23485-25015/z     E/Posting LangId params: 3,
04-21 12:04:12.943 23485-25015/    E/Posting LangId params: 11,
04-21 12:04:12.943 23485-25015/E/Posting Last LangId: 4
04-21 12:04:12.943 23485-25015/E/Posting only 1 subcatId: 15
04-21 12:04:12.943 23485-25015/E/Posting only 1 subcatId: 15
04-21 12:04:12.943 23485-25015/E/Posting subCatId params: 5,
04-21 12:04:12.943 23485-25015/E/Posting last subcatId: 15

1 个答案:

答案 0 :(得分:0)

试试这个,看看它对你有帮助

JSONArray listCatId = new JSONArray();
for (int m = 0; m < Common.Category.size(); m++) {
            if (Common.Category.size() > 1) {
                if (m < Common.Category.size() - 1) {
                    Log.e("Posting Category params", Common.Category.get(m) + ",");
                    listCatId.put(Common.Category.get(m));
//                  params.put("cat_id", Common.Category.get(m) + ",");

                } else {
                    Log.e("Posting cat last params", Common.Category.get(m));
                    params.put("cat_id", Common.Category.get(m));
                }
            } else {
                Log.e("only 1 Cat params", Common.Category.get(m));
                params.put("cat_id", Common.Category.get(m));
            }
        }
        params.put("cat_id",listCatId.toString() );