Http Post on Android: Server Request and Response
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("jchan@lunaweb.com") & "
&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%20开发者_JAVA百科cake”.
- See what are the errors in the logcat file
- Check if you have included the Internet Permission in your manifest file.
never do this
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Look here to see how to do it right: http://source.android.com/source/code-style.html
精彩评论