我是 Android 应用程序的新手。我正在使用的代码在另一台装有 Linux 的计算机上工作。但是在我的电脑上有 Windows 7 并且代码不起作用。它无法连接到网络服务。请帮我。
这是代码:
package com.example.marport;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class Giriscikis extends Activity {
final static String NAMESPACE = "http://webservice.marport.dcat.com/";
final static String METHOD_NAME = "gateOut";
final static String METHOD_NAME2 = "gateIn";
final static String SOAP_ACTION = "http://webservice.marport.dcat.com/gateOut";
final static String SOAP_ACTION2 = "http://webservice.marport.dcat.com/gateIn";
final static String URL = "http://192.168.1.164:8070/test/services/MarportWebServicePort";
private EditText cikis;
private EditText giris;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.giriscikis);
cikis = (EditText) findViewById(R.id.etCikis);
giris = (EditText) findViewById(R.id.etGiris);
SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);
SoapObject Request2 = new SoapObject(NAMESPACE, METHOD_NAME2);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapSerializationEnvelope soapEnvelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = false;
soapEnvelope2.dotNet = false;
soapEnvelope.setOutputSoapObject(Request);
soapEnvelope2.setOutputSoapObject(Request2);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
aht.call(SOAP_ACTION2, soapEnvelope2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
SoapPrimitive result=(SoapPrimitive) soapEnvelope.getResponse();
SoapPrimitive result2 = (SoapPrimitive) soapEnvelope2.getResponse();
giris.setText(result2.toString());
cikis.setText(result.toString());
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以下是例外情况:
08-24 08:25:40.928: E/AndroidRuntime(631): FATAL EXCEPTION: main
08-24 08:25:40.928: E/AndroidRuntime(631): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marport/com.example.marport.Giriscikis}: android.os.NetworkOnMainThreadException
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.os.Looper.loop(Looper.java:137)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-24 08:25:40.928: E/AndroidRuntime(631): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 08:25:40.928: E/AndroidRuntime(631): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 08:25:40.928: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-24 08:25:40.928: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-24 08:25:40.928: E/AndroidRuntime(631): at dalvik.system.NativeStart.main(Native Method)
08-24 08:25:40.928: E/AndroidRuntime(631): Caused by: android.os.NetworkOnMainThreadException
08-24 08:25:40.928: E/AndroidRuntime(631): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.io.IoBridge.connect(IoBridge.java:112)
08-24 08:25:40.928: E/AndroidRuntime(631): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-24 08:25:40.928: E/AndroidRuntime(631): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-24 08:25:40.928: E/AndroidRuntime(631): at java.net.Socket.connect(Socket.java:842)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
08-24 08:25:40.928: E/AndroidRuntime(631): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
08-24 08:25:40.928: E/AndroidRuntime(631): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
08-24 08:25:40.928: E/AndroidRuntime(631): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:146)
08-24 08:25:40.928: E/AndroidRuntime(631): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
08-24 08:25:40.928: E/AndroidRuntime(631): at com.example.marport.Giriscikis.onCreate(Giriscikis.java:53)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.Activity.performCreate(Activity.java:5008)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-24 08:25:40.928: E/AndroidRuntime(631): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-24 08:25:40.928: E/AndroidRuntime(631): ... 11 more