user1437671
user1437671

Reputation:

Android app force closed when trying to send POST data from app to server

I'm really new to Android and Java, I have decent experience in PHP & Web programming. I want to send some POST data from android to my server (Localhost), and display it in the server.

[EDIT UPDATED LOG]

Here's my code:

public class NewUserActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_new_user, menu);
    return true;
}

public void addNewUser(View view) {

    EditText em = (EditText) findViewById(R.id.email);
    EditText na = (EditText) findViewById(R.id.name);
    EditText pa = (EditText) findViewById(R.id.pass);

    String e = em.getText().toString();
    String n = na.getText().toString();
    String p = pa.getText().toString();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost/mapptor/register.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("email", e));
        nameValuePairs.add(new BasicNameValuePair("name", n));
        nameValuePairs.add(new BasicNameValuePair("pass",p));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        @SuppressWarnings("unused")
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException exc) {
        Log.d("T1", "HERE1");
    } catch (IOException exc) {
        Log.d("T2", "HERE2");
    }
}

The problem is, this keeps getting FORCE CLOSED when I run it (emulator).

So I kind of went through the log:

12-30 13:36:54.940: I/ActivityManager(62): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mapptor.android/.NewUserActivity } from pid 130
12-30 13:36:54.980: I/ActivityManager(62): Start proc com.mapptor.android for activity com.mapptor.android/.NewUserActivity: pid=616 uid=10036 gids={}
12-30 13:36:56.110: I/ActivityManager(62): Displayed com.mapptor.android/.NewUserActivity: +1s133ms
12-30 13:37:02.660: W/KeyCharacterMap(116): No keyboard for id 0
12-30 13:37:02.670: W/KeyCharacterMap(116): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 13:37:04.240: W/KeyCharacterMap(616): No keyboard for id 0
12-30 13:37:04.240: W/KeyCharacterMap(616): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 13:37:04.640: D/dalvikvm(116): GC_EXTERNAL_ALLOC freed 254K, 58% free 3570K/8327K, external 7069K/7413K, paused 212ms
12-30 13:37:04.761: W/KeyCharacterMap(116): No keyboard for id 0
12-30 13:37:04.761: W/KeyCharacterMap(116): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-30 13:37:08.690: D/AndroidRuntime(616): Shutting down VM
12-30 13:37:08.690: W/dalvikvm(616): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-30 13:37:08.710: E/AndroidRuntime(616): FATAL EXCEPTION: main
12-30 13:37:08.710: E/AndroidRuntime(616): java.lang.IllegalStateException: Could not execute method of the activity
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.view.View$1.onClick(View.java:2144)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.view.View.performClick(View.java:2485)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.view.View$PerformClick.run(View.java:9080)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.os.Handler.handleCallback(Handler.java:587)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.os.Looper.loop(Looper.java:123)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-30 13:37:08.710: E/AndroidRuntime(616):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 13:37:08.710: E/AndroidRuntime(616):  at java.lang.reflect.Method.invoke(Method.java:507)
12-30 13:37:08.710: E/AndroidRuntime(616):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-30 13:37:08.710: E/AndroidRuntime(616):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-30 13:37:08.710: E/AndroidRuntime(616):  at dalvik.system.NativeStart.main(Native Method)
12-30 13:37:08.710: E/AndroidRuntime(616): Caused by: java.lang.reflect.InvocationTargetException
12-30 13:37:08.710: E/AndroidRuntime(616):  at java.lang.reflect.Method.invokeNative(Native Method)
12-30 13:37:08.710: E/AndroidRuntime(616):  at java.lang.reflect.Method.invoke(Method.java:507)
12-30 13:37:08.710: E/AndroidRuntime(616):  at android.view.View$1.onClick(View.java:2139)
12-30 13:37:08.710: E/AndroidRuntime(616):  ... 11 more
12-30 13:37:08.710: E/AndroidRuntime(616): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
12-30 13:37:08.710: E/AndroidRuntime(616):  at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:572)
12-30 13:37:08.710: E/AndroidRuntime(616):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
12-30 13:37:08.710: E/AndroidRuntime(616):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-30 13:37:08.710: E/AndroidRuntime(616):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-30 13:37:08.710: E/AndroidRuntime(616):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-30 13:37:08.710: E/AndroidRuntime(616):  at com.mapptor.android.NewUserActivity.addNewUser(NewUserActivity.java:61)
12-30 13:37:08.710: E/AndroidRuntime(616):  ... 14 more
12-30 13:37:08.730: W/ActivityManager(62):   Force finishing activity com.mapptor.android/.NewUserActivity

I'm unable to debug this. What could be the issue?

Thank you. :)

Upvotes: 0

Views: 387

Answers (1)

kosa
kosa

Reputation: 66637

HttpPost httppost = new HttpPost("localhost/mapptor/register.php");

Exception message is:

Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.

Based on message it is easy to figure out that:

localhost/mapptor/register.php

is not a valid url. Enter valid HTTP url here. It would be something like:

http://localhost:port/mapptor/register.php

You can make sure that url is valid by accessing it from any web browser.

EDIT:

While working with android localhost lookup you need to use 10.0.2.2 instead of localhost in url. 10.0.2.2 represents loopback IP of the machine and 127.0.0.1 represents loopback IP of the emulator.

Upvotes: 3

Related Questions