0

The Problem

Whenever I click the login button on my mobile app, the app just force closes. There are a ton of errors that fill the console..

Basically what I'm trying to do is send what the user has typed in 2 text boxes to a .php page and get a session key back, this will be used to let the app know the user is logged in.

However, these errors I have no idea how to fix, and I don't know why its just force closing.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#004f00">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:contentDescription="@string/login_logo_desc" 
        android:layout_marginBottom="10dp"/>

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username"
        android:inputType="text"
        android:padding="5dp"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" 
        android:layout_marginBottom="10dp"/>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:padding="5dp"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" 
        android:layout_marginBottom="10dp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:text="@string/button_login"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" 
        android:onClick="login"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

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


@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;
} 
public void login(View v){
    //Create a new HttpClient and Post Header
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://sociobubble.com/m/mobilogin.php"); 
       List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(4);

       try {
           EditText username = (EditText)findViewById(R.id.username); 
           String Username = username.getText().toString(); 
            
           EditText password = (EditText)findViewById(R.id.password); 
           String Password = password.getText().toString(); 
            
           // Add your data
           nameValuePairs.add(new BasicNameValuePair("username", Username));
           nameValuePairs.add(new BasicNameValuePair("password", Password));
           nameValuePairs.add(new BasicNameValuePair("client", "mobile"));

           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

           // Execute HTTP Post Request
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           InputStream is = entity.getContent();
           Log.i("postData", response.getStatusLine().toString());
           
       } catch (IOException e) {
           Log.e("log_tag", "Error in http connection "+e.toString());
       }
}

}

The errors

07-23 18:03:23.833: E/AndroidRuntime(27330): FATAL EXCEPTION: main
07-23 18:03:23.833: E/AndroidRuntime(27330): java.lang.IllegalStateException: Could not execute method of the activity
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.view.View$1.onClick(View.java:3600)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.view.View.performClick(View.java:4106)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.view.View$PerformClick.run(View.java:17052)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.os.Handler.handleCallback(Handler.java:615)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.os.Looper.loop(Looper.java:137)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.app.ActivityThread.main(ActivityThread.java:5059)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.lang.reflect.Method.invoke(Method.java:511)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at dalvik.system.NativeStart.main(Native Method)
07-23 18:03:23.833: E/AndroidRuntime(27330): Caused by: java.lang.reflect.InvocationTargetException
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.lang.reflect.Method.invoke(Method.java:511)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.view.View$1.onClick(View.java:3595)
07-23 18:03:23.833: E/AndroidRuntime(27330):    ... 11 more
07-23 18:03:23.833: E/AndroidRuntime(27330): Caused by: android.os.NetworkOnMainThreadException
07-23 18:03:23.833: E/AndroidRuntime(27330):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-23 18:03:23.833: E/AndroidRuntime(27330):    at com.sociobubble.app.MainActivity.login(MainActivity.java:64)
07-23 18:03:23.833: E/AndroidRuntime(27330):    ... 14 more
Community
  • 1
  • 1
Leighton
  • 103
  • 2
  • 12
  • Tip for you: always, _always_ Google your (last shown) exception when you get an error. In this case, there are tons of questions regarding `NetworkOnMainThreadException`. – davidcesarino Jul 24 '13 at 00:39

2 Answers2

2

You can not make network calls on the main thread. Please read the exception stacktrace and google for NetworkOnMainThreadException

SimonSays
  • 10,867
  • 7
  • 44
  • 59
0

You must asynctask for this typical thing's thats article maybe help you http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

gokhan
  • 627
  • 1
  • 6
  • 16