I was following a tutorial and I got to a point where a lot of the code is deprecated.
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("age", user.age));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParamas.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParamas.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
and another POST method that is returning a result
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
I found that I can replace NameValuePair
with
ContentValues values = new ContentValues();
values.put("name", user.name);
values.put("age", user.age + "");
but I have no idea about the others.
What happens if i continue using Deprecated methods? Code would continue running as it is until method is removed from SDK. If you are using deprecated method then you must keep track of removed apis whenever you upgrade to newest SDK. If you don't want a change at all then check for the reason behind deprecation.
Go to Code > Run Inspection by Name and click on it. A dialog box will open then type deprecated in the text field. You can now select the option based on your search for deprecated usages. On your selection, PHPStorm will automatically search for deprecated usages and list them down for you.
I found that I can replace NameValuePair with
Not really.
but I have no idea about the others
The entire HttpClient API that ships with Android itself is deprecated. The solution is to use a different HTTP client:
HttpUrlConnection
, from standard JavaWith respect to the tutorial, either:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With