从Android应用程序调用简单的Web服务

时间:2016-02-12 01:02:06

标签: android soap

我倾向于android所以我有一个非常简单的Web服务,如果你输入正确的用户名和密码,它会返回你好。

如www.sesgis.com/SayHello用户名和密码:me / pw

<WebService(Namespace:="http://www.sesgis.com/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function SayHello(ByVal User As String, ByVal Password As String, ByVal SomeData As String) As String
    Dim temp As String
    If User = "me" And Password = "pw" Then

        ''Do something with the data at some point
        temp = "Hello"
        Return temp
    Else
        Return Nothing
    End If
End Function
End Class

如何从Android调用此功能?我正在使用android studio并使用我为Okhttp找到的一个例子。我收效甚微。另外,我是否应该将其称为将信息放入URL中?

如果是这样,那会是什么样子 http://www.sesgis.com/SayHello/?User=me&Password=pw

我想,因为我提供了所有可以帮助我使用它的例子的信息。如果我遗漏了任何东西,请告诉我。

感谢您的时间......

编辑...这是我正在使用的Android示例。目前它只返回null。

public class MainActivity extends Activity {

TextView outputText;
Button sendData;
EditText edtUser, edtPass;
final String URL = "http://www.sesgis.com/SayHello/Service.asmx";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    outputText = (TextView) findViewById(R.id.textView1);
    outputText.setMovementMethod(new ScrollingMovementMethod());
    sendData = (Button) findViewById(R.id.button1);
    edtUser = (EditText) findViewById(R.id.editText1);
    edtPass = (EditText) findViewById(R.id.editText2);

    sendData.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String userName = edtUser.getText().toString();
            String passWord = edtPass.getText().toString();
            OkHttpHandler handler = new OkHttpHandler(userName, passWord);
            String result = null;
            try {
                result = handler.execute(URL).get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            outputText.append(result + "\n");
        }
    });
}

和OkHttpHandler.java

public class OkHttpHandler extends AsyncTask<String, Void, String> {

OkHttpClient client = new OkHttpClient();
String userName, passWord;

public OkHttpHandler(String userName, String passWord) {
    // TODO Auto-generated constructor stub
    this.userName = userName;
    this.passWord = passWord;
}

@Override
protected String doInBackground(String... params) {

    RequestBody formBody = new FormEncodingBuilder()
            .add("User", userName)
            .add("Password", passWord)
            .add("SomeData", "TestData")
            .build();
    Request request = new Request.Builder()
            .url(params[0]).post(formBody)
            .build();
    try {
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response.toString());
        return response.body().string();

    } catch (Exception e) {
    }

    return null;
}
}

0 个答案:

没有答案