我在android中使用ksoap2时遇到问题。
我使用ksoap与Web服务进行通信,但它给了我以下错误。代码和异常日志cat如下所示。它在我的代码中的try块的第一行给了我异常。请你帮我。
活动类:
public class MainActivity extends Activity {
public final static String URL = "http://10.0.2.2:8081/Service/services/SqlService.SqlServiceHttpSoap11Endpoint/";
public static final String NAMESPACE = "http://services.com";
public static final String SOAP_ACTION= "http://services.com/locationData";
private static final String METHOD_NAME = "locationData";
String str = null;
TextView t;
String longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
t = (TextView) findViewById(R.id.textView1);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("message","Hello");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SoapPrimitive result;
try {
result = (SoapPrimitive)envelope.getResponse();
String strRes = result.toString();
String resultArr[] = strRes.split("&");
longitude = resultArr[0];
t.setText(resultArr[0]);
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendLocation(View v){
Intent intent = new Intent(MainActivity.this,MappingActivity.class);
intent.putExtra("longitude",longitude);
startActivity(intent);
}
}
logcat的:
> 06-16 09:18:56.590: D/dalvikvm(1293): GC_FOR_ALLOC freed 54K, 5% free
> 2894K/3028K, paused 33ms, total 35ms 06-16 09:18:56.600:
> I/dalvikvm-heap(1293): Grow heap (frag case) to 6.217MB for
> 3480056-byte allocation 06-16 09:18:56.640: D/dalvikvm(1293):
> GC_FOR_ALLOC freed 2K, 3% free 6290K/6428K, paused 39ms, total 39ms
> 06-16 09:18:57.000: W/System.err(1293):
> org.ksoap2.transport.HttpResponseException: HTTP request failed, HTTP
> status: 404 06-16 09:18:57.000: W/System.err(1293): at
> org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:237)
> 06-16 09:18:57.000: W/System.err(1293): at
> org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
> 06-16 09:18:57.000: W/System.err(1293): at
> org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
> 06-16 09:18:57.000: W/System.err(1293): at
> com.example.abc.MainActivity.onCreate(MainActivity.java:53) 06-16
> 09:18:57.000: W/System.err(1293): at
> android.app.Activity.performCreate(Activity.java:5231) 06-16
> 09:18:57.010: W/System.err(1293): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 06-16 09:18:57.010: W/System.err(1293): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 06-16 09:18:57.010: W/System.err(1293): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 06-16 09:18:57.010: W/System.err(1293): at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 06-16
> 09:18:57.010: W/System.err(1293): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 06-16 09:18:57.010: W/System.err(1293): at
> android.os.Handler.dispatchMessage(Handler.java:102) 06-16
> 09:18:57.010: W/System.err(1293): at
> android.os.Looper.loop(Looper.java:136) 06-16 09:18:57.010:
> W/System.err(1293): at
> android.app.ActivityThread.main(ActivityThread.java:5017) 06-16
> 09:18:57.010: W/System.err(1293): at
> java.lang.reflect.Method.invokeNative(Native Method) 06-16
> 09:18:57.010: W/System.err(1293): at
> java.lang.reflect.Method.invoke(Method.java:515) 06-16 09:18:57.010:
> W/System.err(1293): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 06-16 09:18:57.010: W/System.err(1293): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-16
> 09:18:57.010: W/System.err(1293): at
> dalvik.system.NativeStart.main(Native Method) 06-16 09:18:57.010:
> D/AndroidRuntime(1293): Shutting down VM 06-16 09:18:57.010:
> W/dalvikvm(1293): threadid=1: thread exiting with uncaught exception
> (group=0xb3a59ba8) 06-16 09:18:57.030: E/AndroidRuntime(1293): FATAL
> EXCEPTION: main 06-16 09:18:57.030: E/AndroidRuntime(1293): Process:
> com.example.abc, PID: 1293 06-16 09:18:57.030: E/AndroidRuntime(1293):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.abc/com.example.abc.MainActivity}:
> java.lang.NullPointerException 06-16 09:18:57.030:
> E/AndroidRuntime(1293): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> android.app.ActivityThread.access$800(ActivityThread.java:135) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> android.os.Handler.dispatchMessage(Handler.java:102) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> android.os.Looper.loop(Looper.java:136) 06-16 09:18:57.030:
> E/AndroidRuntime(1293): at
> android.app.ActivityThread.main(ActivityThread.java:5017) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> java.lang.reflect.Method.invokeNative(Native Method) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> java.lang.reflect.Method.invoke(Method.java:515) 06-16 09:18:57.030:
> E/AndroidRuntime(1293): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> dalvik.system.NativeStart.main(Native Method) 06-16 09:18:57.030:
> E/AndroidRuntime(1293): Caused by: java.lang.NullPointerException
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> org.ksoap2.serialization.SoapSerializationEnvelope.getResponse(SoapSerializationEnvelope.java:534)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> com.example.abc.MainActivity.onCreate(MainActivity.java:67) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> android.app.Activity.performCreate(Activity.java:5231) 06-16
> 09:18:57.030: E/AndroidRuntime(1293): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
> 06-16 09:18:57.030: E/AndroidRuntime(1293): ... 11 more 06-16
> 09:23:57.430: I/Process(1293): Sending signal. PID: 1293 SIG: 9