通过android模拟器访问asp.net Web服务 [英] Accessing a asp.net web service through android emulator

查看:86
本文介绍了通过android模拟器访问asp.net Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在创建一个简单的Android应用程序,它与我本地计算机的IIS服务器上托管的asp.net Web服务进行通信。我正在尝试使用ip127.0.0.1访问Web服务。

虽然没有编译时错误,但我的代码抛出运行时错误java.net.SocketTimeoutException。我尝试调试我的代码,但无法真正追踪究竟是什么错误。请帮帮我。



我的网络服务连接到数据库并提取传递的ID的名称。我在浏览器中测试了Web服务它运行正常。



还有一个问题。

1.)我们可以连接到网络吗在localhost IIS服务器中托管的服务?



下面是我的MainActivity.java代码。请任何人都可以检查它并为我提供解决方案或提示继续前进。我现在用谷歌搜索了13天,但对我没用。非常感谢帮助,并提前感谢



Hi all,

I'm creating a simple android application which talks to an asp.net web service hosted on my local machine's IIS server. I'm trying to access the web service using the ip "127.0.0.1".
Though no compile time error, my code throws a run time error "java.net.SocketTimeoutException". I tried debugging my code but can't really trace what exactly is wrong. Please help me out with it.

My web service connects to DB and pulls out the name for the id passed. I tested the Web service in browser it works fine.

Also there is a question.
1.) Can we connect to a web service hosted in localhost IIS server?

Below is my MainActivity.java code. Please can anyone check on it and provide me a solution or hint to move ahead. I googled almost for 13 days now but didn't worked for me. Help is really appreciated and thanks in advance

package com.example.getinfo;

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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity 
{
	
	private static final String SOAP_ACTION = "http://tempuri.org/getInfo";
    private static final String OPERATION_NAME = "getInfo";
    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ADDRESS ="http://127.0.0.1:15701/GetInfo.asmx";
    //private static final String SOAP_ADDRESS = "http://127.0.0.1:11970/DBConnect/GetInfo.asmx";
    
    TextView txtData;
    EditText editData;
    Button btnGetData;
    int id;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		txtData = (TextView) findViewById(R.id.txtDisplay);
		btnGetData = (Button) findViewById(R.id.btnGetName);
		
		btnGetData.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View view) 
			{
				SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
				
				 PropertyInfo propertyInfo = new PropertyInfo();
                 propertyInfo.setName("id");
                 propertyInfo.setValue(id);
                 propertyInfo.setType(PropertyInfo.INTEGER_CLASS);
                 
                 editData =(EditText)findViewById(R.id.txtID);
                 id = Integer.parseInt(editData.getText().toString());
                 
                 request.addProperty(propertyInfo);
                 
                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                 envelope.dotNet = true;
                 
                 envelope.setOutputSoapObject(request);
                 
                 HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
                 
                 try  
                 {                    
                     httpTransport.call(SOAP_ACTION, envelope);                    
                     Object response = envelope.getResponse();                    
                     txtData.setText(response.toString());                    
                 }  
                 catch (Exception exception)   
                 {
                     txtData.setText(exception.toString()+"  Or enter number is not Available!");                    
                 }
                 
                 txtData = (TextView)findViewById(R.id.txtDisplay);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

推荐答案

Hello Sujeet,



代码中的localhost / 127.0.0.1指的是运行代码的设备,在本例中是模拟器。尝试使用托管服务的计算机的IP地址。有关详细信息,请访问此处 [ ^ ]



问候,
Hello Sujeet,

The localhost/127.0.0.1 in your code refers to the device on which the code is running, in this case the emulator. Try using the IP address of the machine on which the service is hosted. More information can be found here[^]

Regards,


这篇关于通过android模拟器访问asp.net Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆