我正在尝试建立最终通过 Web 服务将 Android 应用程序连接到数据库。作为中间步骤,我试图查看是否可以让http://codeoncloud.blogspot.com/2012/07/android-php-web-service-client.html正常工作。但是,当我复制/粘贴它并使用 textview 设置适当的布局文件时,尝试运行它时出现错误。具体来说,我的代码如下所示(我试图从同一台机器上的模拟器运行,因此是 10.0.2.2)任何指针将不胜感激!
package com.example.componenttest;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class AndroidWebActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("10.0.2.2/InterfaceTest/GrabMe.php");
try {
TextView tv = (TextView) findViewById(R.id.tv1);
//tv.setText("CHANGED TEXT");
HttpResponse response = httpclient.execute(httppost);
final String str = EntityUtils.toString(response.getEntity());
tv.setText(str);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我的错误日志如下所示:
11-24 22:30:14.976: D/dalvikvm(7269): GC_EXTERNAL_ALLOC freed 53K, 48% free 2807K/5379K, external 1687K/2079K, paused 221ms
11-24 22:30:17.890: D/AndroidRuntime(7269): Shutting down VM
11-24 22:30:17.890: W/dalvikvm(7269): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
11-24 22:30:17.921: E/AndroidRuntime(7269): FATAL EXCEPTION: main
11-24 22:30:17.921: E/AndroidRuntime(7269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.componenttest/com.example.componenttest.AndroidWebActivity}: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.os.Looper.loop(Looper.java:130)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-24 22:30:17.921: E/AndroidRuntime(7269): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 22:30:17.921: E/AndroidRuntime(7269): at java.lang.reflect.Method.invoke(Method.java:507)
11-24 22:30:17.921: E/AndroidRuntime(7269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-24 22:30:17.921: E/AndroidRuntime(7269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-24 22:30:17.921: E/AndroidRuntime(7269): at dalvik.system.NativeStart.main(Native Method)
11-24 22:30:17.921: E/AndroidRuntime(7269): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
11-24 22:30:17.921: E/AndroidRuntime(7269): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:572)
11-24 22:30:17.921: E/AndroidRuntime(7269): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
11-24 22:30:17.921: E/AndroidRuntime(7269): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-24 22:30:17.921: E/AndroidRuntime(7269): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-24 22:30:17.921: E/AndroidRuntime(7269): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-24 22:30:17.921: E/AndroidRuntime(7269): at com.example.componenttest.AndroidWebActivity.onCreate(AndroidWebActivity.java:24)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-24 22:30:17.921: E/AndroidRuntime(7269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-24 22:30:17.921: E/AndroidRuntime(7269): ... 11 more