欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

.NET与Android交互_C#服务器交互接口开发

程序员文章站 2021-12-13 20:43:41
...

本项目是一套 .net web 服务端和安卓客户端交互的实例项目、实现了安卓客户端跟 web 服务端的登录交互等功能、涉及的知识比较基础、C# 的 IIS 服务器配置大家可以上网查一下、数据库请参考model类库中的变量建表、配置好服务器后、到安卓客户端代码中修改服务器 ip 地址就可以正常运行了、非常简单、适合入门的哥们参考一下

.NET与Android交互_C#服务器交互接口开发


客户端登录代码

/*
 * 登陆检查用户id和密码
 */
private void startUrlCheck() {
    // 打开网络连接
    HttpClient client = new DefaultHttpClient();
    StringBuilder builder = new StringBuilder();

    // 服务器提交地址
    HttpGet myget = new HttpGet(ServerUrl   "login.ashx?type=login&UserId="
              EditText1.getText().toString()   "&UserPwd="
              EditText2.getText().toString());
    try {

        HttpResponse response = client.execute(myget);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        // 填充数据流
        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
            builder.append(s);
        }
        // 读取json返回数组
        JSONObject jsonObject = new JSONObject(builder.toString());
        String re_result = jsonObject.getString("Result");
        String re_UserID = jsonObject.getString("UserId");
        String re_MoneyWallet = jsonObject.getString("MoneyWallet");
        String re_MoneyBank = jsonObject.getString("MoneyBank");

        if (re_result.equals("1")) {

            Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT)
                    .show();
            Intent intent = new Intent();
            Bundle Bundle1 = new Bundle();
            intent.setClass(MainActivity.this, MoneyActivity.class);
            Bundle1.putString("UserID", re_UserID);
            Bundle1.putString("MoneyWallet", re_MoneyWallet);
            Bundle1.putString("MoneyBank", re_MoneyBank);
            intent.putExtras(Bundle1);
            startActivity(intent);

        } else {

            Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT)
                    .show();

        }

        progressDialog1.dismiss();

    } catch (Exception e) {
        Log.v("url返回", "false");
        e.printStackTrace();
        Toast.makeText(MainActivity.this, "服务器数据读取失败", Toast.LENGTH_SHORT)
                .show();
        progressDialog1.dismiss();
    }
}


服务器登录代码

/// <summary>
/// 判断登陆
/// </summary>
/// <param name="context"></param>
private void login(HttpContext context)
{
    if (bllTuserInfo.Exists(long.Parse(context.Request["UserId"].ToString()),
        context.Request["UserPwd"].ToString()))
    {
        modelTuserInfo = bllTuserInfo.GetModel(long.Parse(context.Request["UserId"].ToString()));

        context.Response.Write("{"Result":"1","UserId":""   modelTuserInfo.UserId   "","MoneyWallet":""   modelTuserInfo.MoneyWallet   "","MoneyBank":""   modelTuserInfo.MoneyBank   ""}");
    }
    else
    {
        context.Response.Write("{"Result":"0"}");
    }
}


源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1jHjvVCi 密码: t3pg