First time asking a question here, and new to android programming. I'm following an online youtube tutorial to create a login by user "Tonikami TV". Everything is fine with the app except when it comes to the serverRequests class. I get that NameValuePair
, HttpParams
, etc. are deprecated which I understand to be outdated and unsupported since API 22. I've searched for some fixed or alternatives but can't really make sense of them and how I would apply them to my code. Any help would be greatly appreciated. Thanks :)
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("firstname", user.FirstName));
dataToSend.add(new BasicNameValuePair("lastname", user.LastName));
dataToSend.add(new BasicNameValuePair("age", user.Age + ""));
dataToSend.add(new BasicNameValuePair("emailaddress", user.EmailAddress));
dataToSend.add(new BasicNameValuePair("password", user.Password));
//possible alternative code found on stack overflow don't know exactly what to do from here.
ContentValues values= new ContentValues();
values.put("firstname", user.FirstName);
values.put("lastname", user.LastName);
values.put("age", user.Age + "");
values.put("emailaddress",user.EmailAddress);
values.put("password",user.Password);
//
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpClient post = new HttpPost(SERVER_ADDRESS + "Register.php");
try{
post.setEntity(new URLEncoderFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
Here is the entire code in the ServerRequests class. Apologies if its rather long.
public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://lok8.hostingsiteforfree.com";
public ServerRequests(Context context){
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please Wait...");
}
public void storeUserDataInBackground(User user, GetUserCallback userCallback){
progressDialog.show();
new StoreUserDataAsyncTask(user,userCallback).execute();
}
public void fetchUserDataInBackground(User user, GetUserCallback callback) {
progressDialog.show();
new fetchUserDataAsyncTask(user, callback).execute();
}
public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void>{
User user;
GetUserCallback userCallback;
public StoreUserDataAsyncTask(User user, GetUserCallback userCallback){
this.user = user;
this.userCallback = userCallback;
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("firstname", user.FirstName));
dataToSend.add(new BasicNameValuePair("lastname", user.LastName));
dataToSend.add(new BasicNameValuePair("age", user.Age + ""));
dataToSend.add(new BasicNameValuePair("emailaddress", user.EmailAddress));
dataToSend.add(new BasicNameValuePair("password", user.Password));
//possible alternative code found on stack overflow don't know exactly what to do from here.
ContentValues values= new ContentValues();
values.put("firstname", user.FirstName);
values.put("lastname", user.LastName);
values.put("age", user.Age + "");
values.put("emailaddress",user.EmailAddress);
values.put("password",user.Password);
//
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpClient post = new HttpPost(SERVER_ADDRESS + "Register.php");
try{
post.setEntity(new URLEncoderFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
userCallback.done(null);
super.onPostExecute(aVoid);
}
}
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallback;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
this.user = user;
this.userCallback = userCallback;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("emailaddress", user.EmailAddress));
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);
HttpClient post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
User returnedUser = null;
try{
post.setEntity(new URLEncoderFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if(jObject.length()== 0){
returnedUser = null;
} else {
String firstname = jObject.getString("firstname");
String lastname = jObject.getString("lastname");
int age = jObject.getInt("age");
returnedUser = new User(firstname, lastname, age, user.FirstName, user.LastName, user.Age);
}
}catch (Exception e){
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallback.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
}
You can use the following code which uses the standard java and android methods
The getEncodedData method (Simple to understand)