NameValuePair deprecated for openConnection

前端 未结 7 1198
梦谈多话
梦谈多话 2020-12-01 06:09

I been following online tutorials on how to insert data into a database from an android app and have everything working except this small part

List

        
7条回答
  •  温柔的废话
    2020-12-01 06:52

    you can use contentvalues or hashmap based on your preference.

    i have used Content values

    ContentValues contentValues = new ContentValues();
    contentValues.put("key1","value1");
    contentValues.put("key2","value2");
    

    and if the data that u are posting is form data then here`s how u can convert it form data

     public String getFormData(ContentValues contentValues) throws UnsupportedEncodingException {
    
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Map.Entry entry : contentValues.valueSet()) {
            if (first)
                first = false;
            else
                sb.append("&");
    
            sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            sb.append("=");
            sb.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
        }
        return sb.toString();
    }
    

提交回复
热议问题