android - 连接到Web服务

时间:2016-03-24 08:07:14

标签: android sql-server web-services asmx somee

我正在尝试将android应用程序连接到某个上传的Web服务。

我有以下问题:

致命的例外:主要                                                                                  java.lang.RuntimeException:无法启动活动ComponentInfo:java.lang.NullPointerException

网络服务代码:

wp_

我在清单文件中添加了以下行:     

** MainActivity.java文件**

[WebService(Namespace = "http://www.n-m.somee.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
    SqlConnection con = new SqlConnection(@"workstation id=WSDatabase.mssql.somee.com;packet size=4096;user id=***;pwd=***;data source=WSDatabase.mssql.somee.com;persist security info=False;initial catalog=WSDatabase ");
    [WebMethod]
    public string get_name(String pssw)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select username from users where pssw="+pssw, con);
        string rd =(string) cmd.ExecuteScalar();

        con.Close();
        return rd;
    }
}

我感谢任何帮助 谢谢

1 个答案:

答案 0 :(得分:1)

你的网址有点不对劲。这是我用的,

protected String namespace = "http://www.nourhan-m.somee.com/";
protected String url = "http://www.nourhan-m.somee.com/WebService1.asmx";
protected String SOAPAction = "http://www.nourhan-m.somee.com/get_name";
protected String method_name = "get_name";

我对您的代码进行了一些修改以使其正常工作。

public String callService(String username) {
    //create soap object and add params to it
    SoapObject request = new SoapObject(namespace, method_name);
    request.addProperty("pssw", username);

    //create envelop
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

    //set request to envelope
    envelope.bodyOut = request;

    HttpTransportSE transport = new HttpTransportSE(url);
    transport.debug = true;
    try {
        transport.call(SOAPAction, envelope);
        Log.e("OUT", transport.requestDump);
        return transport.responseDump;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (Exception e){
        e.printStackTrace();
    }
    return "";
}

这就是我调用方法的方法,

您可以按如下方式调用它,

final String username = (TextView) findViewById(R.id.textView1)

new Thread(new Runnable() {
    @Override
    public void run() {
        Log.e("OUT", callService(username));
    }
}).start();

我得到了一个空的回复,也许是因为我没有提供有效的用户名。看看它。干杯:)