NameValuePair deprecated for openConnection

前端 未结 7 1186
梦谈多话
梦谈多话 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:40

    If you realy want to use NameValuePair in your application, you can add the useLibrary 'org.apache.http.legacy' to your gradle:

    buildtypes{
        //------------
        //----------
    }
    
    useLibrary 'org.apache.http.legacy'
    
    0 讨论(0)
  • 2020-12-01 06:41

    I just ran into the same problem. The deprecated classes from org.apache.http have been removed in API 23.

    I ended up using android.util.Pair. It works perfectly, and the code is shorter too:

    List<Pair<String, String>> params = new ArrayList<>();
    params.add(new Pair<>("username", username));
    params.add(new Pair<>("password", password));
    
    0 讨论(0)
  • 2020-12-01 06:45

    Alternate to NameValuePair. Also you can get the name and values from it as mentioned below. Here key isa name.

    Create:

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

    Get key and value :

    for (Map.Entry<String, Object> entry : values.valueSet()) {
        String key = entry.getKey(); // name
        String value = entry.getValue().toString(); // value
    }
    
    0 讨论(0)
  • 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<String, Object> 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();
    }
    
    0 讨论(0)
  • 2020-12-01 06:54

    You can use httpmime.jar file instead of it, which will work better that NameValuePair. You can Download it from here, Download

    MultipartEntity multi = new MultipartEntity();
    multi.addPart("name", new StringBody("Raj"));
    multi.addPart("Id", new StringBody("011"));
    

    add this jar to your project and then use it.

    0 讨论(0)
  • 2020-12-01 06:55

    Yo can change your android Api in 21, right click, properties android, click in api 21 is work for me

    0 讨论(0)
提交回复
热议问题