来自android的Webservice调用表示权限被拒绝

时间:2013-12-07 11:32:00

标签: android .net web-services

我在.net中有一个Web服务,我在localhost中从android调用它。它是在模拟器显示中显示一个字符串,但它不起作用。它显示模拟器中的访问被拒绝。

SoapTestActivity.java

package com.sample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;



public class SoapTestActivity extends Activity {
    TextView result;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        result = (TextView)findViewById(R.id.result);

        final String NAMESPACE = "http://sample.com/";
        final String METHOD_NAME = "SayHello";    
        final String SOAP_ACTION = "http://sample.com/SayHello";
        final String URL = "http://192.168.1.108/WebService1/Service1.asmx";

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            String resultValue = response.toString();

            result.setText(resultValue);           
        }
        catch (Exception e) {
            result.setText(e.getMessage());
        }
    }
}

的manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sample.SoapTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我在firefox浏览器中显示的网址是

http://localhost:5460/Service1.asmx

1 个答案:

答案 0 :(得分:0)

请将网址192.168.1.108更改为10.0.2.2:80

更改此设置后,您将在模拟器上获取数据

请参阅http://developer.android.com/tools/devices/emulator.html#networkaddresses以获取更多参考资料。