从android接收数据到aspx页面并返回响应
问题描述:
我想从我的android应用程序发送一些数据到在android中使用HttpClient的aspx页面。我将发送我的用户名和密码在aspx页面内进行验证。这是我做过什么对Android的一面:从android接收数据到aspx页面并返回响应
//安卓侧
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.8.38/someapplication/logRemote.aspx");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
Log.d("extraData", "running");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
我这样做的原因是因为我要检查,我从机器人传递的数据将被ASPX阅读页面代码在后面。
如何使用从Android发送的字符到Aspx页面?
这是我做的aspx页面上:
//的.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginRemoteWeb.aspx.cs" Inherits="LoginRemoteWeb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
和后面的代码(网页加载部分):
//后面的代码
protected void Page_Load(object sender,EventArgs e) {
//Request.Form to be sent by values from android
int loop1;
coll = Request.Form;
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Form: " + arr1[loop1] + "<br>");
}
}
我在做什么正确的?我能使用Request.Form从android获得postdata吗?如果是这样,它会丢回我回复给Android的回应吗?
答
您可以在aspx页面中创建webmethod。你也可以在android中设置httppost或httpget的webmethod属性作为你的需求。