无法从Android应用程序访问我的asp.net webservice

时间:2014-11-10 05:34:48

标签: c# android asp.net web-services ksoap2

我正在尝试从Android应用程序访问ASP.net webservice。我正在使用KSOAP并成功访问http://www.w3schools.com/webservices/tempconvert.asmxhttp://www.webservicex.net/ConvertWeight.asmx webservices。现在我创建了自己的web服务来访问我自己的数据库并将其上传到somee.com,但是当我尝试访问它们时,它返回一个空值。

这是我的android代码:

package com.webservice.retrieve;

import android.os.AsyncTask;  
import android.os.Bundle;
import android.app.Activity;
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 com.prguru.android.R;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
   private final String NAMESPACE = "http://myservice.somee.com/Service.asmx/";
   private final String URL = "http://myservice.somee.com/Service.asmx";
   private final String SOAP_ACTION = "http://myservice.somee.com/Service.asmx/userName";
   private final String METHOD_NAME = "userName";
   private static String retrieve;
   private static String result;
   Button b;
   TextView tv;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv_result);
    b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                AsyncCallWS task = new AsyncCallWS(); 
                task.execute();
        }
    });
}

public void getFahrenheit() {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    String e_mail = "k102144@nu.edu.pk";


    PropertyInfo weightProp =new PropertyInfo();
    weightProp.setName("e_mail");
    weightProp.setValue(e_mail);
    weightProp.setType(String.class);
    request.addProperty(weightProp);

    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();
        result = response.toString();

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

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        Log.i("ABC", "doInBackground");
        getFahrenheit();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i("ABC", "onPostExecute");
        tv.setText(result + "");
    }

    @Override
    protected void onPreExecute() {
        Log.i("ABC", "onPreExecute");
        tv.setText("Retrieving...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i("ABC", "onProgressUpdate");
    }

  }

}

0 个答案:

没有答案