androidhttpclientdeprecatedhttpentity

Alternative methods for HttpClient, HttpPost, HttpResponse, HttpEntity, etc in Android


I'm trying to make a login application that gets data from an online database. I've found a tutorial on the internet, but most of the methods are deprecated. So I'd like to know how I can find alternative methods for the deprecated ones.

After I adapted the code on the tutorial to my project, I have this code on my MainActivity:

public class MainActivity extends AppCompatActivity {

    Button b1;
    EditText ed1,ed2;

    public static final String USER_NAME = "USERNAME";

    String username;
    String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1 = (EditText) findViewById(R.id.editText);
        ed2 = (EditText) findViewById(R.id.editText2);
    }

    public void invokeLogin(View view){

        username = ed1.getText().toString();
        password = ed2.getText().toString();

        login(username,password);
    }

    private void login(final String username,String password){

        class LoginAsync extends AsyncTask<String, Void, String>{

            private Dialog loadingDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog = ProgressDialog.show(MainActivity.this,"Please wait","Loading...");
            }

            @Override
            protected String doInBackground(String... params){
                String uname = params[0];
                String pass = params[1];

                InputStream is = null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username",uname));
                nameValuePairs.add(new BasicNameValuePair("password",pass));

                String result = null;

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://example.com/login.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    BufferReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (ClientProtocolExeption e){
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e){
                    e.printStackTrace();
                }catch (IOException e){
                    e.printStackTrace();
                }

                return result;
            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

So I'm getting the "Cannot resolve method/symbol" for the following ones:

NameValuePair
BasicNameValuePair
HttpClient
DefaultHttpClient
HttpPost
httpPost.setEntity
UrlEncondedFormEntity
HttpResponse
httpClient.execute
HttpEntity
response.getEntity
entity.getContent
BufferReader
reader.readLine
ClientProtocolExeption
e.printStackTrace

I'm pretty new to Android development and I'm lost here.

I've tried to replace this:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username",uname));
                nameValuePairs.add(new BasicNameValuePair("password",pass));

With that (am I correct):

HashMap<String,String> nameValuePairs = new HashMap<>();
                nameValuePairs.put("username",params[0]);
                nameValuePairs.put("password",params[1]);

Solution

  • You'll want to check out my answer to this question. I explain how to use HttpURLConnection to make a web request and how to set up the necessary callbacks to get the response data.