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!