我想从android手机发送信息到服务器。Android上的Http发布:服务器请求和响应
当服务器收到信息时,它发回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
}
}
网站是早些时候宣布的变量。它是一个包含表单接收器的位置的字符串
也低于是我得到了有关服务器的信息
您将与(最有可能)一个“内容 - 发送一个“POST”请求键入“application/x-www-form-urlencoded”。
“POST”的内容将是一个格式与网站网址的“查询>字符串”类似的字符串。 (如果你不熟悉它们,这就是问号后面的内容。)
在这个字符串中,有些键和值之间用等号连接。键/值对由>符号分隔(&'s)。
这是我在ASP用于测试服务,如果它有助于(具有>添加用于可读性回车)的字符串的一个例子:
strContent =“dateOfEventStart = 2011-09-24
& dateOfEventEnd = 2011-09-26
&的locationType = “& Server.URLEncode( ”住宅“)&”
& locationZipCode = 38016
& EVENTTYPE = “& Server.URLEncode( ”企业活动“)& ”
& eventTypeSecondary =
& groundSurface =“ & Server.URLEncode(” 碎石(超过污垢) “)&”
& groundSurfaceSecondary =
个& numberOfGuests = 90个
& seatingArrangements =” &服务器。的URLEncode( “6位客人回合表”)& “
& accessoriesTables =” & Server.URLEncode( “自助,蛋糕,礼品,饮料站”)&“
& accessoriesEntertainment =
& estimatedArea =“& Server.URLEncode(”1200平方英尺“)&”
& estimatedTentSize =“& Server.URLEncode( “30英尺×40英尺或20英尺×60英尺”)& “
& estimatedRoomToSpare =” & Server.URLEncode( “0平方英尺或0平方英尺”)&“
& CONTACTNAME = “& Server.URLEncode( ”乔纳森陈“)&”
& contactPhone = 9011234567
& contactEmail =” & Server.URLEncode( “[email protected]”)& “
& contactComments =” & Server.URLEncode( “这是一个长注释”。)
以我ASP例如你可能已经注意到了“Server.URLEncode”周围的字符串。这是>所以某些可能会混淆数据的字符会被编码为%十六进制ASCII>值。例如,如果有人输入“我喜欢芝士&蛋糕”作为他的评论,该程序>会认为该&符号表示新的键/值对。如果我们对网址进行编码,它会显示为“I%20love%20cheese%20%26%20cake”。
好吧,我检查的权限,以及Internet权限不在那里,我补充道。它现在与服务器进行通信,但表单不能正常工作。它收到字符串,但似乎没有处理它 – Zaask