xamarin用get的方法把json值传给php写的服务端登录页

20 篇文章 0 订阅
13 篇文章 3 订阅

      最近,抽空写了个用xamarin写个android传json给php服务端的程序,作为登录页使用,需要system.json组件,php端直接get到数据放进json里,不多说了,上代码,先搞个php服务端的,局域网内可以跨网段使用,亲测。

这是服务端上 6.php页面代码
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_GET['name']) && isset($_GET['password']) )
{
  
   $name = $_GET['name'];
   $pwd = $_GET['password'];
   if($name=="aaa" & $pwd=="222")
  
  {
       // successfully inserted into database
       $response["success"] = 1;
       $response["message"] = "sucess";
       // echoing JSON response
       echo json_encode($response);
       
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "成功";
fwrite($myfile, $txt);
fclose($myfile);
   } 
   else
    {
       // failed to insert row
       $response["success"] = 0;
       $response["message"] = "name or password is wrong";
       
       // echoing JSON response
       echo json_encode($response);
         
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "用户名或密码错误";
fwrite($myfile, $txt);
fclose($myfile);

   }

else 
{
   // required field is missing
   $response["success"] = 0;
   $response["message"] = "the require is not compition ";
   // echoing JSON response
   echo json_encode($response);
    
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "缺少";
fwrite($myfile, $txt);
fclose($myfile);
   
}
?>

xamarin安卓端的代码如下:


先来个ui界面的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <TextView
            android:text="用户名:"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView1" />
        <EditText
            android:inputType="textMultiLine"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/editText1" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3">
        <TextView
            android:text="密码:"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView2" />
        <EditText
            android:inputType="date"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/editText2" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:gravity="center">
        <Button
            android:text="登录"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button1" />
    </LinearLayout>
</LinearLayout>

MainActivity.cs的代码


using Android.App;
using Android.Widget;
using Android.OS;
using System.Json;
using System.Net;
using System.IO;
using System;
using Android.Content;
using System.Linq;




namespace App10
{
    [Activity(Label = "App10", MainLauncher = true)]
    public class MainActivity : Activity
    {
        Button btn_login;
        EditText txt_name;
        EditText txt_pwd;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            btn_login = FindViewById<Button>(Resource.Id.button1);
            txt_name = FindViewById<EditText>(Resource.Id.editText1);
            txt_pwd = FindViewById<EditText>(Resource.Id.editText2);
            btn_login.Click += Btn_lgoin_Click;
        }


        private void Btn_lgoin_Click(object sender, System.EventArgs e)
        {
            string name = txt_name.Text;
            string pwd = txt_pwd.Text;
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd))
            {
                Toast.MakeText(this, "请输入用户名和密码!!", ToastLength.Long).Show();
                return;
            }
            else
            {
                 //JSON请求URL GET的方式
                string url = "http://192.168.0.45:8081/jsontest/6.php?name="+name+"&password="+pwd;
                //创建一个请求
                var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
              httpReq.BeginGetResponse(new AsyncCallback(ReadXamarin), httpReq);
            }
        }
        //异步回调方法
         public void ReadXamarin(IAsyncResult asyn)
         {
             var httpReq = (HttpWebRequest)asyn.AsyncState;
             
           //获取响应
            using (var httpRes = (HttpWebResponse) httpReq.EndGetResponse(asyn))
             {
                 //判断是否成功获取响应
               if (httpRes.StatusCode == HttpStatusCode.OK)
                 {
                     //读取响应
                     var text = new StreamReader(httpRes.GetResponseStream()).ReadToEnd();
                     //切换到UI线程,否则无法对控件进行操作
                     RunOnUiThread(() =>
                     {
                         Toast.MakeText(this, text, ToastLength.Long).Show();
                     });
                }
             }
        }
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Xamarin 是一个跨平台的移动开发框架,可以用它来开发 iOS、Android 和 Windows 的应用。要用 Xamarin 一个计算器,需要具备一定的编程知识和 Xamarin 的相关知识。 ### 回答2: 使用Xamarin可以利用C#语言来编一个计算器应用程序。首先,可以创建一个Xamarin.Forms项目,该项目将允许开发人员使用共享的代码库来构建跨平台的应用程序。 在创建项目后,可以在XAML文件中设计应用程序的用户界面。可以添加按钮、文本框和标签等控件来创建计算器的界面。 接下来,可以在C#代码中实现计算器的逻辑。可以在按钮的点击事件处理程序中编代码来处理用户的输入和计算。例如,当用户点击一个数字按钮时,可以将相应的数字添加到计算器的显示文本框中。当用户点击运算符按钮时,可以将其存储为当前的操作符,并将当前输入的数字存储为操作数。当用户点击等号按钮时,可以根据当前的操作符进行相应的计算并更新显示文本框的结果。 此外,还可以添加其他功能,如清除按钮用于清除当前操作数和操作符,以及小数点按钮用于处理小数计算等。 最后,可以在Android和iOS设备上运行和测试应用程序。Xamarin提供了强大的跨平台支持,开发人员可以在一个代码库中构建适用于不同操作系统的应用程序。 总之,使用Xamarin可以方便地编一个计算器应用程序,并实现基本的计算功能。通过共享代码库,可以轻松地将应用程序运行在多个平台上,并为用户提供一个易于使用和功能齐全的计算器应用程序。 ### 回答3: Xamarin是一种跨平台的移动应用开发框架,可以使用C#语言来开发iOS、Android和Windows平台的应用程序。使用Xamarin可以实现一个简单的计算器应用程序。 首先,我们可以使用Xamarin.Forms来创建一个跨平台的用户界面。在XAML文件中定义计算器的布局,包括按钮、标签和输入框等。可以设置按钮的点击事件来响应用户输入。 接着,我们可以编C#代码实现计算器的逻辑。定义一个Calculator类,包含各种运算方法,例如加法、减法、乘法和除法等。根据用户的输入,在按钮点击事件中调用相应的计算方法,并更新显示结果的标签。 在iOS和Android平台上,需要适配不同的屏幕大小和分辨率。可以使用Xamarin的屏幕适配特性,例如自动布局和约束布局,来确保计算器的界面在不同平台上都能正确显示。 最后,使用Xamarin的调试工具可以在模拟器或真机上运行和测试应用程序。可以检查用户输入的合法性,并给出相应的错误提示。同时,可以进行性能优化,确保计算器应用程序的运行流畅且稳定。 总而言之,使用Xamarin可以方便地编一个跨平台的计算器应用。通过XAML文件定义界面布局,使用C#编逻辑代码,结合Xamarin的屏幕适配和调试工具,可以实现一个功能完善的计算器应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值