Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameValuePair deprecated for openConnection

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.

like image 392
user1949387 Avatar asked Mar 31 '15 20:03

user1949387


3 Answers

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)); 
like image 193
friederbluemle Avatar answered Sep 20 '22 06:09

friederbluemle


You can use ContentValues, for example:

ContentValues values = new ContentValues();
values.put("username", name);
values.put("password", password);
database.insert(Table_name, null, values);
like image 30
Menna-Allah Sami Avatar answered Sep 20 '22 06:09

Menna-Allah Sami


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
}
like image 23
Fahim Avatar answered Sep 19 '22 06:09

Fahim