-1

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!

Salivan
  • 1,876
  • 7
  • 26
  • 45

1 Answers1

1

You are running a network related operation on the ui thread. Use Thread or AsycTask.

HttpResponse response = httpClient.execute(httpPost); // must be executed in a thread
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Are you sure this is the only problem? However I'm gonna try to fix it immediately! Thanks! – Salivan Mar 04 '14 at 18:01
  • @user2883597 make the changes and if you run into problem post the stacktrace. I guess currently you have a `NetworkOnMainThreadException` – Raghunandan Mar 04 '14 at 18:02
  • Wow! I managed to do what you adviced me and it seems it works perfectly! I'll now try to fetch some data from the webpage I have logged in to ;] Thanks a BUNCH!!! – Salivan Mar 04 '14 at 18:40
  • Maybe you could help me with getting some information from the webpage? I managed to get webpage's source code, but I need particular lines. For example, I want to get this line: "LATEST NEWS" from this webpage: http://www.sammobile.com/. How can I do this? – Salivan Mar 04 '14 at 19:25