0

我真的是 Android 和 Java 的新手,我在 PHP 和 Web 编程方面有不错的经验。我想将一些 POST 数据从 android 发送到我的服务器(本地主机),并将其显示在服务器中。

[编辑更新日志]

这是我的代码:

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");
    }
}

问题是,当我运行它(模拟器)时,它会一直强制关闭。

所以我浏览了日志:

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

我无法调试这个。可能是什么问题?

谢谢你。:)

4

1 回答 1

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

异常消息是:

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

根据消息很容易弄清楚:

本地主机/mapptor/register.php

不是有效的网址。在此处输入有效的 HTTP 网址。它会是这样的:

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

您可以通过从任何 Web 浏览器访问它来确保该 url 有效。

编辑:

在使用 android localhost 查找时,您需要在 url 中使用 10.0.2.2 而不是 localhost。10.0.2.2代表机器的loopback IP,127.0.0.1代表模拟器的loopback IP。

于 2012-12-31T17:00:26.663 回答