I'm trying to login to a webpage and see if login was successful but my app crashes. Here's my code:
package com.shortey.logtoweb;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void login (View view) {
try {
final String LOGIN_LINK = "https://www.manodienynas.lt/";
final String MY_EMAIL = "********"; //here should be my username
final String MY_PW = "********"; //here should be my password
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(LOGIN_LINK);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("username", MY_EMAIL));
nameValuePairs.add(new BasicNameValuePair("password", MY_PW));
nameValuePairs.add(new BasicNameValuePair("submit_login", "Prisijungti"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
TextView TV1 = (TextView)findViewById(R.id.textView1);
if (response.getStatusLine().getStatusCode() < 400) {
TV1.setText("Success");
} else {
TV1.setText("Failed");
}
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I'm calling the method login() by pressing a button. Please help me to fix mistakes in my code that makes my app crash. I know i didn't handle exceptions properly but I guess this doesn't cause my app to crash but maybe would help to find the problem. How to handle them properly in my case? Thanks in advance!