我是Android应用程序的新手。我正在使用的代码正在另一台拥有Linux的计算机上工作。但是在我的电脑上有Windows 7,代码无效。它无法连接到webservice。请帮帮我。
这是代码:
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
答案 0 :(得分:2)
申请人停止因为..
Caused by: android.os.NetworkOnMainThreadException
当应用程序尝试在其主线程上执行网络操作时引发的异常。查找有关此NetworkOnMainThreadException的详细信息
<强>解决方案:强>
使用AsyncTask并将与您的网络相关的操作放在doInVBackground()
中..
答案 1 :(得分:1)
您正在主UI线程上的该Activity中执行一些网络活动。每当您的构建目标设置为蜂窝或更高时,这将引发异常。要解决此问题,请将网络操作移至AsyncTask。
示例:
public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
private Activity activity;
private ProgressDialog progressDialog;
public MyAsyncTask(Activity activity) {
super();
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
}
@Override
protected Result doInBackground(Void... v) {
//do your stuff here
return null;
}
@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
Toast.makeText(activity.getApplicationContext(), "Finished.",
Toast.LENGTH_LONG).show();
}
}
从活动中调用它:
MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();
答案 2 :(得分:0)
听起来你正试图在UI线程中发出http请求。从Android 3.0开始,您必须将您的请求放在另一个帖子中。
查看帖子:HonyComb and DefaultHttpClient
我也建议你使用try / catch。
答案 3 :(得分:0)
您可以通过在setcontentview行之后添加它来解决此问题:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
我不推荐它,如果你已经完成了对代码的测试,请使用AsyncTask。
答案 4 :(得分:0)
您是否尝试过停用Windows 7防火墙?
教奶奶吃鸡蛋(有时对奶奶有帮助)=&gt; http://windows.microsoft.com/en-US/windows-vista/Turn-Windows-Firewall-on-or-off#