NameValuePair deprecated for openConnection
Asked Answered
F

7

33

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<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));

NameValuePair and BasicNameValuePair have been deprecated in favor of openConnection(). How can I create a new-value association with that? http://developer.android.com/reference/java/net/URL.html#openConnection()

Does anyone know how I can create name value pairs with openConnection? I been searching everywhere.

Freer answered 31/3, 2015 at 20:32 Comment(0)
W
32

You can use ContentValues, for example:

ContentValues values = new ContentValues();
values.put("username", name);
values.put("password", password);
database.insert(Table_name, null, values);
Waterside answered 1/4, 2015 at 15:8 Comment(3)
ContentValues has been design in order to be used by contentresolver NOT network communications - which the question is based on - since there is no getName() and getValue() methods in case you need to parse header attributes.Conventionalism
@Conventionalism you can get the name and values from ContentValues https://mcmap.net/q/57913/-namevaluepair-deprecated-for-openconnectionBedrabble
To me creating an iterator to get the key-values like that seems like marginally extra overhead - I prefer @Wunder 's answer belowPreservative
W
45

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));
Wunder answered 12/10, 2015 at 3:39 Comment(5)
Thanks, there is also a support library implementation for backwards compatibilityPreservative
This is the correct way to achieve this in API level 23Cussedness
One thing to note here: Using android.util.Pair will create a dependency on the Android framework, so if you want "plain Java" objects that are easily unit testable etc. you can simply use Map<String, String> - although that does not allow duplicate keys (which is possible for HTTP headers according to RFC2616).Wunder
Just to add if you want to get the values as in the example above you can use (String) params.first and (String) params.secondMarlee
this seems to be the better alternative and hence the accepted answer IMHO.Addend
W
32

You can use ContentValues, for example:

ContentValues values = new ContentValues();
values.put("username", name);
values.put("password", password);
database.insert(Table_name, null, values);
Waterside answered 1/4, 2015 at 15:8 Comment(3)
ContentValues has been design in order to be used by contentresolver NOT network communications - which the question is based on - since there is no getName() and getValue() methods in case you need to parse header attributes.Conventionalism
@Conventionalism you can get the name and values from ContentValues https://mcmap.net/q/57913/-namevaluepair-deprecated-for-openconnectionBedrabble
To me creating an iterator to get the key-values like that seems like marginally extra overhead - I prefer @Wunder 's answer belowPreservative
B
7

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
}
Bedrabble answered 14/9, 2015 at 7:15 Comment(2)
Why can't I use Map<String,String> values = new Hashmap<>(); then? What is the advantage of using ContentValues?Convulsion
@Convulsion you can use whichever u prefer, my answer was to point hesam comment in accepted answerBedrabble
I
2

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();
}
Ivied answered 24/9, 2016 at 13:22 Comment(0)
M
1

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.

Malefaction answered 22/6, 2015 at 9:25 Comment(0)
D
1

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'
Detainer answered 17/5, 2016 at 6:56 Comment(0)
A
-46

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

Acroterion answered 5/4, 2015 at 4:49 Comment(3)
Changing the compile version and the target is not a solution, just a workaround that will prevent you from using new APIs.Bilharziasis
functions are depreciated as typically a newer, more improved alternative has been included. its counter productive and just incorrect to use this method. id recommend either Content Values as Menna-Allah Sami has provided, or Map<String, String>Myke
NameValuePair is deprecated in API ver 22. It means It will not use in future.Albertinaalbertine

© 2022 - 2024 — McMap. All rights reserved.