android - WCF With Android- 绑定(bind)结果与标签或 ListView

标签 android asp.net wcf listview adapter

  1. 我创建了一个带有 2 个参数用户名和密码的 WCF。
  2. 我创建了一个 android 用户界面,它接受输入的用户名和密码,并且在按钮单击事件中它的用户名和密码存在我已经为标签分配了一个静态值“登录成功”。

我需要指导如何将结果集与标签绑定(bind),而不是将静态值传递给标签。

公共(public)类 JSONSampleAppActivity 扩展 Activity 实现 OnClickListener{ /** 在第一次创建 Activity 时调用。 */

//Property declaration
Button btnLogin;
TextView lblStatus;
EditText txtUserName,txtPassword;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnLogin=(Button)findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(this);

    lblStatus=(TextView)findViewById(R.id.lblStatus);

    txtUserName=(EditText)findViewById(R.id.txtUserName);
    txtPassword=(EditText)findViewById(R.id.txtPassword);
}

@Override
public void onClick(View v) {

    switch(v.getId())
    {
        case R.id.btnLogin:
            String userName=txtUserName.getText().toString();
            String password=txtPassword.getText().toString();
            if(verifyLogin(userName,password))
            {

                lblStatus.setText("Login Successfully");
            }
            else
            {
                lblStatus.setText("Login Failed");
            }
            break;
    }
}

public static String convertStreamToString(InputStream is) 
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } 
    catch (IOException e) {
        e.printStackTrace();
    } 
    finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

public static boolean verifyLogin(String UserName,String Password)
{
    try
    {
        System.out.println("guru");
        DefaultHttpClient httpClient=new DefaultHttpClient();

        //Connect to the server
        HttpGet httpGet=new HttpGet("http://xxxx/Service1.svc/checkLogin?name="+UserName+"&pass="+Password);
        //Get the response
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream stream=httpEntity.getContent();

        //Convert the stream to readable format
        String result= convertStreamToString(stream);

        if(result.charAt(1)=='1')
        {

            return true;
        }
        else
        {
            return false;
        }
    }
    catch(Exception e)
    {
        return false;
    }

}

最佳答案

To get the result set in label your code should look like this :

public static String verifyLogin(String UserName,String Password)
{
    try
    {
        System.out.println("guru");
        DefaultHttpClient httpClient=new DefaultHttpClient();

        //Connect to the server
        HttpGet httpGet=new HttpGet("http://xxxx/Service1.svc/checkLogin?name="+UserName+"&pass="+Password);
        //Get the response
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream stream=httpEntity.getContent();

        //Convert the stream to readable format
        String result= convertStreamToString(stream);



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

call above method like this in onclick:

            String userName=txtUserName.getText().toString();
            String password=txtPassword.getText().toString();
            String res=verifyLogin(userName,password)
            lblStatus.setText(res);

whatever result is will be displayed in textview.

关于android - WCF With Android- 绑定(bind)结果与标签或 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23434352/

相关文章:

android - 将变量传递给 ViewBinder

android - JNI 在应用程序中检测到错误 : JNI NewGlobalRef called with pending exception java. lang.ClassNotFoundException:

c# - SignalR 和 UploadFromStreamAsync 在 IIS Express 和 Azure 免费网站中具有不同的行为

asp.net - 测试如何与 ASP.NET MVC/WEBFORMS Silverlight MVVM 相关

c# - 如何使用我的 InstanceProvider 使用自己的构造函数调用 WCF 服务

android - PhoneGap : How do I get appView's id and pass it around?

android - 一次发射一个项目,与之交互直到满足条件,然后继续下一个项目

asp.net - Request.Form ["name"] 什么时候为空,什么时候为空字符串?

wpf - VS2017中WPF项目中的WCF数据服务引用

c# - 如何降低 WCF 命名管道的完整性