1

I am trying to make an app that would login the website.

I am completely new to this kind of stuff, but so far the research on that topic has brought me to this code. But I am not sure where to go from here.

Like the main task I need to do is:

Get the username/password that a person has entered in the app; Use it to try and enter the website; If successful, switch to another activity and show necessary information there, else show toast "login failed".

The most important part is the process of sending and receiving information of username/password (it is the one I cannot figure out).

public class MainActivity extends AppCompatActivity {

public void doLogin(View v){
    String username = ((EditText)findViewById(R.id.editTextUsername)).getText().toString();
    String password = ((EditText)findViewById(R.id.editTextPassword)).getText().toString();

    try{
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);

        HttpClient httpClient = new DefaultHttpClient(params);
        HttpPost httpPost = new HttpPost("https://my.network.lviv.ua/");

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

        httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();

        String responseString = EntityUtils.toString(entity);

        Toast.makeText(getApplicationContext(),responseString, Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),"Login Failed!",Toast.LENGTH_SHORT).show();
    }
}

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

When I put in username and password, despite them being correct, it shows: Login Failed!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vitaliy-T
  • 733
  • 6
  • 23
  • What happens with your current approach? – Xenolion Jan 13 '18 at 21:26
  • @Xenolion when I put in username and password, despite them being correct, it shows: Login Failed! – Vitaliy-T Jan 13 '18 at 21:30
  • @Xenolion gonna take a look at it, thanks. Another question, do I need anything from the website itself except the url? I mean the source code or database access. – Vitaliy-T Jan 13 '18 at 21:38
  • 1
    The website might be giving different responses if the password and user names are correct. And giving a different response if one or both are wrong. You will have to parse the html response to figure out that! – Xenolion Jan 13 '18 at 21:41

0 Answers0