teej2542
teej2542

Reputation: 608

Android Studio - NameValuePair and HttpParams is deprecated

I am new to Android development and I am have some deprecated issues. In Android Studio, it is stating that the NameValuePair and HttpParams is deprecated.

ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("fname", user.fname));
        dataToSend.add(new BasicNameValuePair("lname", user.lname));
        dataToSend.add(new BasicNameValuePair("email", user.email));
        dataToSend.add(new BasicNameValuePair("password", user.password)); 

HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS + "register");

When searching for answers, I am getting sent to use openconnection(), but I don't see how that applies to code above.

Upvotes: 0

Views: 11557

Answers (5)

Hamid
Hamid

Reputation: 1563

just adding the dependencies in gradle:

dependencies{
  implementation'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

Upvotes: 0

Please add the below dependencies for both NameValuePair & Httpclient to make this work.

dependencies{
      compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

Upvotes: 1

ked
ked

Reputation: 2456

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();
}

Upvotes: 1

user1581412
user1581412

Reputation:

You have to add the dependencies of HttpClient in your build.gradle file:

android {

compileSdkVersion 23

buildToolsVersion "22.0.1"

useLibrary 'org.apache.http.legacy'

...

}

Upvotes: 3

TheLibrarian
TheLibrarian

Reputation: 1888

Did you try to use ContentValues ?

From this snippet of code I am not sure if it would help you.

ContentValues values=new ContentValues();

  values.put("fname",user.fname);
  values.put("lname", user.lname);
  values.put("email",user.email);
  values.put("password",user.password);

Upvotes: 2

Related Questions