您的位置:首页 > 移动开发 > Android开发

Android利用ashx生成登陆界面

2011-11-10 09:07 267 查看
首先需要在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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: