0

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. :)

1 Answers1

3
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.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • @SuhasSG: It could be for number of reasons, first of all, have you validated the url in browser? – kosa Dec 31 '12 at 17:10
  • And my server is up and running, so that shouldn't be an issue correct? And when I send POST data from some other PHP, it just executes perfectly. So no issue with server or address. –  Dec 31 '12 at 17:10
  • Yes, I have validated. It runs fine when I send POST variables by other means, and execute. –  Dec 31 '12 at 17:11
  • @SuhasSG: Do you get exactly the same exception when you fix the URL? – Codo Dec 31 '12 at 17:17
  • Yes, but now I have that exception, and MORE exceptions in the log. –  Dec 31 '12 at 17:19
  • @SuhasSG: Don't you need any port in URL? Are you sure same url working from your web browser? Update exception stack also. – kosa Dec 31 '12 at 17:23
  • No port. The exact same url is working fine from web browser. :) But android is getting force closed. :( Yeah the stack is updated. –  Dec 31 '12 at 17:29
  • `2-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)` Could this be something? –  Dec 31 '12 at 17:31
  • May be try with 127.0.0.1 (or) machine IP? instead of localhost? And clean your project after save. – kosa Dec 31 '12 at 17:35
  • See this link, it is worth to make sure this is not an issue http://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips – kosa Dec 31 '12 at 17:38
  • Okay, now it's not force-closing anymore. :D But I'm still not receiving the data in my server. That may be some other issue. Is my POST DATA sending using JAVA correct? Well, anyway. THANKS. Had to use 10.0.2.2 –  Dec 31 '12 at 17:45
  • @SuhasSG: I would make sure on php side you are reading correct parameters. If answer helped you don't forget to accept it. Good luck. – kosa Dec 31 '12 at 17:49