【问题标题】:android - connect to web serviceandroid - 连接到网络服务
【发布时间】:2016-03-24 08:07:14
【问题描述】:

我正在尝试将 android 应用程序连接到在 somee 上上传的网络服务。

我有以下问题:

致命异常:主要 java.lang.RuntimeException: 无法启动活动 ComponentInfo: java.lang.NullPointerException

网络服务代码:

[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;
    }
}

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

** MainActivity.java 文件**

public class MainActivity extends AppCompatActivity {

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

TextView user_name;
@SuppressLint("NewApi")

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}


public void goButton(View v)
{
    user_name=(TextView)findViewById(R.id.textView1);
    //allow internet
    if(Build.VERSION.SDK_INT>9)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    //create soap object
    SoapObject soapObject=new SoapObject(namespace,method_name);
    soapObject.addProperty("pssw", "1234");

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

    //Rrquest get data from webservice and set it into soapobject

    HttpTransportSE transport = new HttpTransportSE(url);

    try {
        transport.call(SOAPAction, envelope);

        // get data
        SoapPrimitive output=(SoapPrimitive) envelope.getResponse();
        //set data into control
        user_name.setText("pssw: "+output.toString());

    } catch (IOException e) {
        e.printStackTrace();
     } catch (XmlPullParserException e) {
        e.printStackTrace();
    }

}
}

感谢任何帮助 谢谢

【问题讨论】:

  • 您的数据库连接似乎有些错误。
  • 当我将连接字符串放入 web.config 并将 SOAPAction 更改为 SOAPAction="n-m.somee.com/get_name";感谢您的帮助,但现在错误是:在 20000 毫秒后无法连接到 www.n-m.somee.com/208.94.246.108(端口 80)

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


【解决方案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();

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

【讨论】:

  • 感谢您的帮助。但是您的代码或我的代码修改后返回null我不知道为什么,我尝试了Web主机上的Web服务sql查询,它返回值
  • 网络服务获取 pssw 并返回用户名 try "1234"
  • 它什么也没给我..<get_nameResponse xmlns="http://www.nourhan-m.somee.com/" />。空洞的回应。 :(
  • @n-m 在你的数据库中,是pssw 字段varchar?
  • pssw 字段为 nvarchar
猜你喜欢
  • 2014-01-14
  • 2013-11-18
  • 1970-01-01
  • 2014-10-10
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
相关资源
最近更新 更多