Ian DC Bautista February 2016

HttpParams is deprecated. What should I do?

I'm trying to make a Login and register on my android app. I'm having trouble with HttpParams. I am following a tutorial on Youtube by Tonikami. I'm just new with Android Studio. This is just the process I know. Your help with be very appreciated. :(

public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://mywebsite.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 fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
    progressDialog.show();
    new fetchUserDataAsyncTask(user, userCallBack).execute();
}

/**
 * parameter sent to task upon execution progress published during
 * background computation result of the background computation
 */

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("name", user.name));
        dataToSend.add(new BasicNameValuePair("username", user.username));
        dataToSend.add(new BasicNameValuePair("password", user.password));
        dataToSend.add(new BasicNameValuePair("age", user.age + ""));

        HttpParams httpRequestParams = getHttpRequestParams();

        HttpClient client = new DefaultHttpClient(http        

Answers


Aziz February 2016

URL url = new URL("http://google.co.in");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(2000);
           is = conn.getInputStream();

Use the above code instead of depreciated httpParams

And for passing post params, do like this

 Uri.Builder builder = new Uri.Builder();
    builder.appendQueryParameter("param1", "paramvalue1");
            builder.appendQueryParameter("param2", "paramvalue2");

String query = builder.build().getEncodedQuery();
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();
            is = conn.getInputStream();

Post Status

Asked in February 2016
Viewed 1,322 times
Voted 9
Answered 1 times

Search




Leave an answer