簡體   English   中英

如何從Android調用.net Web服務?

[英]How to call .net webservice from android?

我在Visual Studio中創建了一個演示.net Web服務,並嘗試使用localhost在本地瀏覽器中運行。 這給出了以下錯誤之一:

1)當我的地址= IP +端口號時,XML拉解析器異常

2)當我的地址=仿真器端口+ localport時,未知主機異常

3)當我的地址= localhost +端口號時,連接異常無法連接本地主機

如何使用CMD創建AVD?

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();
                       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM