现在的位置: 首页 > 移动开发 > 正文

Android利用ashx生成登陆界面

2019年01月14日 移动开发 ⁄ 共 2602字 ⁄ 字号 评论关闭

首先需要在AndroidManifest.xml中加上:

<uses-permission android:name="android.permission.INTERNET" />

然后添加两个输入框和一个按钮:

<EditText android:id="@+id/UserName"
  android:layout_width="100dip" android:layout_height="30dip"/>
<EditText android:id="@+id/Password"
  android:layout_width="100dip" android:layout_height="30dip"/>
<Button android:id="@+id/LoginButton"
  android:layout_width="100dip" android:layout_height="30dip"
  android:text="Login" />

之后重载OnCreate方法:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        etUserName = (EditText)findViewById(R.id.UserName);
        etPassword = (EditText)findViewById(R.id.Password);
        tvDisplay = (TextView)findViewById(R.id.display);
       
        Button loginButton = (Button)findViewById(R.id.LoginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    String responseString = LoginRequest();
    tvDisplay.setText(responseString);
   }
  });
    }

最后增加返回函数:

 private String LoginRequest()
 {
  StringBuilder response = new StringBuilder();
        try
        {          
         StringBuilder xml =  new StringBuilder();
         xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
         xml.append("<Request xmlns=\"http://nextgen.huawei.com/api\">");
         xml.append("<Name>Login</Name>");
         xml.append("<Paramenters><Parameter name=\"UserName\" dataType=\"string\">"+ etUserName.getText()
           +"</Parameter><Parameter name=\"Password\" dataType=\"string\">" + etPassword.getText()
           +"</Parameter></Paramenters>");
         xml.append("</Request>");
         byte[] xmlbyte = xml.toString().getBytes("UTF-8");
         URL url = new URL("http://192.167.0.33/NextMobileInterface.ashx");
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setDoOutput(true);
         conn.setRequestMethod("POST");       
         conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length));
         conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
         DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
         outStream.write(xmlbyte);
         outStream.flush();

         if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
         {
          BufferedReader input = new BufferedReader(
           new InputStreamReader(conn.getInputStream()),8192);
          String strLine = null;
          while((strLine = input.readLine())!= null)
          {
           response.append(strLine);
          }
         }
        }
        catch(Exception ex)
        {
         return ex.toString();
        }
        return response.toString();
 }

 

抱歉!评论已关闭.