there is a request to the server, form a line and insert certain data, try to insert data from the list, but it seems to be a crutch, how else can I insert data?

public void formationShoppingList(ArrayList<Model> list) { String temp=""; String str; for (int i = 0; i < list.size(); i++) { str = "<ProductName" + i + ">" + list.get(i).getName_tovar() + "</ProductName" + i + ">\n" + "<ProductBrand" + i + ">" + list.get(i).getMark_tovar() + "</ProductBrand" + i + ">\n" + "<ProductPrice" + i + ">" + list.get(i).getCost_tovar() + "</ProductPrice" + i + ">\n"; temp+=str; } Log.d("TAG", temp); } 

    1 answer 1

    It is possible through the Map.

     Map<String,String> params = new HashMap<String, String>(); params.put("name1", "value1"); params.put("name2", "value2"); 

    Further params write in OutputStream

     OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(getPostDataString(params));// функция которая делает строку, ниже ее код writer.flush(); writer.close(); os.close(); 

    Here is the function preparing the string.

     private String getPostDataString(Map<String,String> params) throws UnsupportedEncodingException{ StringBuilder result = new StringBuilder(); boolean first = true; for(Map.Entry<String, String> entry : params.entrySet()){ if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } return result.toString(); } 

    Based on this issue . There in more detail.

    • I read that question, but again it turns out that params.put ("name1", "value1"); params.put ("name2", "value2"); we have to prescribe with our hands, this is not a problem, but I may have 10 name, or maybe 1. here’s how to catch this moment - Sergey
    • @ Sergey, wait, you, in any case, your parameters should be initially set, even if it is 10 pieces or 1 piece. In what form do you have it originally? Or where are you getting them from and in what form? - Ilya Shashilov
    • I have an alert dialog through which I enter data, then they are stored in my arraylist, and yes I can take the number of elements from the list, but it is different every time .. or something I don’t understand .. - Sergey
    • @ Sergey understood, your user himself creates the parameters, which you then send to the server. Number of parameters may be different? Data from users get into Arraylist, i.e. He can say it is created programmatically and its size can be different each time. You then steam your Arraylist into a string, your decision does not seem to be a crutch, why does it bother you? Thus, it is possible to record the results of alertDialoga in the Map and use the solution I suggested. In any case, the result will be the string that you will drive into the outputStream. - Ilya Shashilov
    • I just wondered if it was possible to do something more beautiful, sort of how to describe the structure of the request 1 time, and submit the list to the input right away and she should parse the fields herself .. apparently the library should be looked at - Sergey