【问题标题】:How to call .net webservice from android?如何从android调用.net webservice?
【发布时间】:2013-11-11 12:03:59
【问题描述】:

我在 Visual Studio 中创建了一个演示 .net Web 服务,并尝试使用 localhost 在本地浏览器中运行。这给出了以下错误之一:

1) 当我的地址 = IP + 端口号时 XML 拉取解析器异常

2) 我的地址 = 模拟器端口 + 本地端口时出现未知主机异常

3) 当我的地址 = localhost + 端口号时,连接异常连接本地主机失败

如何使用 CMD 创建 AVD?

【问题讨论】:

  • 私有静态最终字符串 SOAP_ADDRESS = "my ip:56044//Service1.asmx";私有静态最终字符串 SOAP_ADDRESS = "http://10.0.2.2:8080//Service1.asmx";私有静态最终字符串 SOAP_ADDRESS = "localhost:56044/Service1.asmx";当我一一使用这个地址时,应用程序根据我的问题给出了三个不同的错误,我使用 IIS express 和浏览器在 localhost 上运行我的服务。什么是完美的解决方案。
  • 我的程序无法运行,因为 localhost 无法被模拟器理解?
  • 我也试过这个代码stackoverflow.com/questions/1048310/…,但找不到完美的解决方案!!!提前致谢。

标签: java android asp.net web-services


【解决方案1】:
If You Want to call Rest Webservice than Here Is the Code:



class LongOperation extends AsyncTask<String, String, String>{

            @Override
            //  http://stackoverflow.com/questions/3505930/make-an-http-request-with-android
            protected String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return responseString;
            }




If You Want to call WCF/asmx Webservice than Here Is the Code:
Here You have to use KSOAP2 library

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "[http://10.0.2.2:8085/Service1.svc][1]";
//Use Service1.asmx for asmx service
private final String SOAP_ACTION = "http://tempuri.org/GetData";
private final String METHOD_NAME = "GetData";


                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                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("myApp_Responce...", response.toString());

                    TextView tv= (TextView)findViewById(R.id.textView1);
                    tv.setText(response.toString());
                      } catch (Exception e)
                      {
                          e.printStackTrace();
                       }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多