Android 上的 Http Post:服务器请求和响应
我正在尝试从 Android 手机向服务器发送信息。
当服务器收到信息时,它会发回 1 或 0 来表示通过或失败。服务器端一切都很好,因为另一个 iOS 应用程序可以做同样的事情,但它可以工作。服务器还会在收到请求时发送电子邮件。
我的问题是应用程序似乎没有联系服务器。当我运行该应用程序时,执行 Http Post 代码后不会返回任何响应,也不会发送任何电子邮件。
我有下面的 Http Post 代码,感谢您提供的任何帮助。
public void send()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(site);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("dateOfEventStart", startYear + "-" + startMonth + "-" + startDay));
nameValuePairs.add(new BasicNameValuePair("dateOfEventEnd", endYear + "-" + endMonth + "-" + endDay));
nameValuePairs.add(new BasicNameValuePair("locationType", locType));
nameValuePairs.add(new BasicNameValuePair("locationZipCode", location));
nameValuePairs.add(new BasicNameValuePair("eventType", type));
nameValuePairs.add(new BasicNameValuePair("groundSurface", groundType));
nameValuePairs.add(new BasicNameValuePair("numberOfGuests", numGuests + ""));
nameValuePairs.add(new BasicNameValuePair("seatingArrangments", arrangement));
nameValuePairs.add(new BasicNameValuePair("accessoriesTables",stuff));
nameValuePairs.add(new BasicNameValuePair("estimatedArea", tent));
nameValuePairs.add(new BasicNameValuePair("estimatedRoomToSpare", spared));
nameValuePairs.add(new BasicNameValuePair("contactName", nameA));
nameValuePairs.add(new BasicNameValuePair("contactPhone", phoneA));
nameValuePairs.add(new BasicNameValuePair("contactEmail", addressA));
nameValuePairs.add(new BasicNameValuePair("contactComments", comment));
nameValuePairs.add(new BasicNameValuePair("isInternational", isInternational + ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseString = response.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
site 是之前声明的变量。它是一个包含表单接收者位置的字符串,
下面也是我获得的有关服务器的信息
您将向其发送一个“POST”请求,其“内容类型”(很可能)为“application/x-www-form-urlencoded”。
“POST”的内容将是一个字符串,其格式类似于网站 URL 的“Query > String”。 (如果您不熟悉,那就是问号后面的内容。)
在此字符串中,键和值通过等号连接。键/值对由 & 符号 (&'s) 分隔。
下面是我在 ASP 中用来测试服务是否有帮助的字符串示例(添加了 > 回车符以提高可读性):
strContent =“dateOfEventStart=2011-09-24
&dateOfEventEnd=2011-09-26
&locationType=" & Server.URLEncode("住宅") & "
&位置邮政编码=38016
&eventType=" & Server.URLEncode("企业活动") & "
&eventTypeSecondary=
&groundSurface=" & Server.URLEncode("砾石(泥土上)") & "
&groundSurfaceSecondary=
&客人数量=90
&seatingArrangements=" & Server.URLEncode("6位客人圆桌") & "
&accessoriesTables=" & Server.URLEncode("自助餐、蛋糕、礼品、饮料站") & "
&配件娱乐=
&estimatedArea=" & Server.URLEncode("1200 平方英尺") & "
&estimatedTentSize=" & Server.URLEncode("30 英尺 x 40 英尺或 20 英尺 x 60 英尺") & "
&estimatedRoomToSpare=" & Server.URLEncode("0 平方英尺或 0 平方英尺") & "
&contactName=" & Server.URLEncode("Jonathan Chan") & "
&contactPhone=9011234567
&contactEmail=" & Server.URLEncode("[电子邮件受保护]< /a>") & "
&contactComments=" & Server.URLEncode("这是一条很长的评论。")
在我的 ASP 示例中,您可能已经注意到字符串周围有“Server.URLEncode”。这是&gt;因此某些可能会弄乱数据的字符被编码为%十六进制ASCII&gt;值。例如,如果有人输入“我喜欢奶酪和奶酪”。 cake” 作为他的评论,程序 > 会认为 & 符号表示一个新的键/值对。如果我们对 URL 进行编码,它将看起来像“I%20love%20cheese%20%26%20cake”。
I am trying to send information to a server from an android phone.
When the server receives the information it sends back a 1 or a 0 to signify pass or fail. Server-side everything is fine because there in another app for iOS that does the same thing, but it works. Also the sever sends an email anytime it receives a request.
My problem is that it does not seem like the app is contacting the server. When I run the app no response is given back and no email is sent once the Http Post code is executed.
I have the Http Post code below, thank you for any help you can give .
public void send()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(site);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("dateOfEventStart", startYear + "-" + startMonth + "-" + startDay));
nameValuePairs.add(new BasicNameValuePair("dateOfEventEnd", endYear + "-" + endMonth + "-" + endDay));
nameValuePairs.add(new BasicNameValuePair("locationType", locType));
nameValuePairs.add(new BasicNameValuePair("locationZipCode", location));
nameValuePairs.add(new BasicNameValuePair("eventType", type));
nameValuePairs.add(new BasicNameValuePair("groundSurface", groundType));
nameValuePairs.add(new BasicNameValuePair("numberOfGuests", numGuests + ""));
nameValuePairs.add(new BasicNameValuePair("seatingArrangments", arrangement));
nameValuePairs.add(new BasicNameValuePair("accessoriesTables",stuff));
nameValuePairs.add(new BasicNameValuePair("estimatedArea", tent));
nameValuePairs.add(new BasicNameValuePair("estimatedRoomToSpare", spared));
nameValuePairs.add(new BasicNameValuePair("contactName", nameA));
nameValuePairs.add(new BasicNameValuePair("contactPhone", phoneA));
nameValuePairs.add(new BasicNameValuePair("contactEmail", addressA));
nameValuePairs.add(new BasicNameValuePair("contactComments", comment));
nameValuePairs.add(new BasicNameValuePair("isInternational", isInternational + ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseString = response.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
site is variable that was declared earlier on. It is a string that contains location of the form receiver
also below is the information I was given about the server
You will send it a “POST” request with (most likely) a “content-type” of “application/x-www-form-urlencoded”.
The content of the “POST” will be a String that is formatted like a website URL’s “Query >String”. (That’s the stuff after the question mark if you’re not familiar with them.)
In this string, there are keys and values connected by an equal sign. Key/Value pairs are >separated by ampersands (&’s).
Here is an example of a string I used in ASP to test the service if it helps (with >carriage returns added for readability):
strContent = "dateOfEventStart=2011-09-24
&dateOfEventEnd=2011-09-26
&locationType=" & Server.URLEncode("Residential") & "
&locationZipCode=38016
&eventType=" & Server.URLEncode("Corporate Event") & "
&eventTypeSecondary=
&groundSurface=" & Server.URLEncode("Gravel (over dirt)") & "
&groundSurfaceSecondary=
&numberOfGuests=90
&seatingArrangements=" & Server.URLEncode("Round tables of 6 guests") & "
&accessoriesTables=" & Server.URLEncode("Buffet,Cake,Gift,Beverage Station") & "
&accessoriesEntertainment=
&estimatedArea=" & Server.URLEncode("1200 sq ft") & "
&estimatedTentSize=" & Server.URLEncode("30 ft x 40 ft or 20 ft x 60 ft") & "
&estimatedRoomToSpare=" & Server.URLEncode("0 sq ft or 0 sq ft") & "
&contactName=" & Server.URLEncode("Jonathan Chan") & "
&contactPhone=9011234567
&contactEmail=" & Server.URLEncode("[email protected]") & "
&contactComments=" & Server.URLEncode("This is a long comment.")
In my ASP example, you may have noticed “Server.URLEncode” surrounding Strings. This is >so certain characters that could mess up the data gets encoded into % hexadecimal ASCII >values. For example, if someone enters “I love cheese & cake” as his comment, the program >will think that the ampersand is denoting a new Key/Value pair. If we encode the URL, it >will look like “I%20love%20cheese%20%26%20cake”.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切勿这样做
请查看此处,了解如何正确执行此操作:http://source.android. com/source/code-style.html
never do this
Look here to see how to do it right: http://source.android.com/source/code-style.html