0

我是 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
4

5 回答 5

2

应用力停止,因为..

Caused by: android.os.NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。 查找有关此NetworkOnMainThreadException的更多信息

解决方案:

使用AsyncTask并将您的网络相关操作放入doInVBackground()其中。

于 2012-08-24T08:59:09.137 回答
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();
于 2012-08-24T08:57:25.727 回答
0

您是否尝试过禁用 Windows 7 防火墙?

教奶奶吸鸡蛋(有时对奶奶有帮助)=> http://windows.microsoft.com/en-US/windows-vista/Turn-Windows-Firewall-on-or-off#

于 2012-08-24T09:05:12.733 回答
0

您可以通过在 setcontentview 行之后添加它来解决它:

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

不过我不推荐它,如果你已经完成了代码测试,请使用 AsyncTask。

于 2012-08-24T09:01:18.573 回答
0

听起来您正试图在 UI 线程中发出 http 请求。从 Android 3.0 开始,您必须将您的请求放在另一个线程中。

看到那个帖子:HonyComb 和 DefaultHttpClient

我还建议你使用 try/catch。

于 2012-08-24T08:57:23.300 回答