0

我是在 android 中开发项目的新手。我尝试使用 KSOAP 从 android 获取 .NET Web 服务。但是当我从 web 服务收到值时,我得到输出 null°F 作为输出。请在我的程序中找出错误。

package com.example.webservice;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WebService extends Activity {
       private final String NAMESPACE = "http://tempuri.org/";
       private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?wsdl";
       private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
       private final String METHOD_NAME = "CelsiusToFahrenheit";
    Button b;
    TextView tv;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_service);
        et=(EditText)findViewById(R.id.editText1);
        tv=(TextView)findViewById(R.id.Result);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            String result=getFarenheit(et.getText().toString());
            tv.setText(result+"°F");
            }
        });
    }
    public String getFarenheit(String celsius){
        SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo celsuiusPI= new PropertyInfo();
        celsuiusPI.setName("Celsius");
        celsuiusPI.setValue(celsius);
        celsuiusPI.setType(double.class);
        request.addProperty(celsuiusPI);
        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
        try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
            Log.i("WebService output", response.toString());
            return response.toString();

        }
        catch(Exception e){
            e.printStackTrace();
        }

        return null;
    }

}

输入和输出图像

在此处输入图像描述 在此处输入图像描述

日志猫:

11-15 15:31:42.215: W/System.err(1058): android.os.NetworkOnMainThreadException
11-15 15:31:42.215: W/System.err(1058):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-15 15:31:42.215: W/System.err(1058):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-15 15:31:42.215: W/System.err(1058):     at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
11-15 15:31:42.225: W/System.err(1058):     at libcore.io.IoBridge.connect(IoBridge.java:112)
11-15 15:31:42.225: W/System.err(1058):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-15 15:31:42.225: W/System.err(1058):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-15 15:31:42.225: W/System.err(1058):     at java.net.Socket.connect(Socket.java:842)
11-15 15:31:42.235: W/System.err(1058):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
11-15 15:31:42.235: W/System.err(1058):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:117)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-15 15:31:42.246: W/System.err(1058):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-15 15:31:42.255: W/System.err(1058):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-15 15:31:42.265: W/System.err(1058):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
11-15 15:31:42.265: W/System.err(1058):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:120)
11-15 15:31:42.265: W/System.err(1058):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:176)
11-15 15:31:42.265: W/System.err(1058):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
11-15 15:31:42.275: W/System.err(1058):     at com.example.webservice.WebService.getFarenheit(WebService.java:58)
11-15 15:31:42.275: W/System.err(1058):     at com.example.webservice.WebService$1.onClick(WebService.java:41)
11-15 15:31:42.275: W/System.err(1058):     at android.view.View.performClick(View.java:4084)
11-15 15:31:42.285: W/System.err(1058):     at android.view.View$PerformClick.run(View.java:16966)
11-15 15:31:42.285: W/System.err(1058):     at android.os.Handler.handleCallback(Handler.java:615)
11-15 15:31:42.285: W/System.err(1058):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 15:31:42.285: W/System.err(1058):     at android.os.Looper.loop(Looper.java:137)
11-15 15:31:42.305: W/System.err(1058):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-15 15:31:42.305: W/System.err(1058):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 15:31:42.305: W/System.err(1058):     at java.lang.reflect.Method.invoke(Method.java:511)
11-15 15:31:42.315: W/System.err(1058):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-15 15:31:42.315: W/System.err(1058):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
4

1 回答 1

2

当我们尝试从主线程访问网络服务时发生 NetworkOnMainThread 异常

为避免这种情况,您必须在子线程、Handler 或 asyncTask 中调用 Web 服务,您的问题将得到解决

于 2012-11-15T10:06:13.207 回答