I’m trying to build up to eventually connecting an Android application to a database via a webservice. As an intermediate step, I was trying to see if I could get http://codeoncloud.blogspot.com/2012/07/android-php-web-service-client.html working correctly. However, when I copy/paste that and set up the appropriate layout file with a textview, I get an error when trying to run it. Specifically, my code is shown below (I’m trying to run from an emulator on the same machine hence the 10.0.2.2) Any pointers would be greatly appreciated!
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();
}
}
}
My error log is shown below:
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
Try changing
to
I highly recommend the nuSOAP PHP library for creating a SOAP server. Perhaps try something more specific for the Android client such as this library: https://code.google.com/p/droidsoapclient/. If you are planning to use SOAP that is… =)