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();
}
<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();
}
相关文章推荐
- Android利用reative_layout生成梅花界面
- Android利用reative_layout生成梅花界面
- Android的登陆界面生成验证码
- 利用半透明对话框实现新浪微博android客户端首次运行时的提示界面
- android中进行布局管理界面_一、利用ListView进行布局
- 利用半透明对话框实现新浪微博android客户端首次运行时的提示界面
- Android自定义控件实现登陆界面以及SharedPreferences实现记住密码功能
- 利用ubuntu将.cpp生成二进制的可执行文件,然后利用.bat(脚本)编写命令,将可执行文件在android机上运行
- 使用层级观察器hierarchyviewer综合各种布局实现Android QQ2012登陆界面和注册界面
- Java EE登陆界面生成随机数防止恶意注册或者登录
- android-利用xml解析和生成数据
- 利用DevExpress的LayoutControl控件实现界面的自动生成
- Android 利用Gson生成或解析json
- Android APP 登陆界面开发 UI设计
- android 如何实现登陆界面的记住密码功能
- LedPedometer-3.注册界面和Android随机生成图片验证码
- Android仿qq登陆界面
- 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- linux(以ubuntu为例)下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件