Reputation: 608
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
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
Reputation: 357
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
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
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
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