Android上的Http Post:服务器请求和响应

时间:2011-06-22 16:45:38

标签: android http-post httprequest

我正在尝试从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&gt; String”。 (如果你不熟悉问题,这就是问号之后的东西。)

     

在此字符串中,键和值由等号连接。键/值对由&符号(&amp; s)分隔。

     

以下是我在ASP中用于测试服务的字符串示例(如果有帮助的话)(为了可读性添加了&gt;回车符):

     

strContent =“dateOfEventStart = 2011-09-24

     

&安培; dateOfEventEnd = 2011-09-26

     

&amp; locationType =“&amp; Server.URLEncode(”Residential“)&amp;”

     

&安培; locationZipCode = 38016

     

&amp; eventType =“&amp; Server.URLEncode(”Corporate Event“)&amp;”

     

&安培; eventTypeSecondary =

     

&amp; groundSurface =“&amp; Server.URLEncode(”Gravel(over dirt)“)&amp;”

     

&安培; groundSurfaceSecondary =

     

&安培; numberOfGuests = 90

     

&amp; seatingArrangements =“&amp; Server.URLEncode(”6位客人的圆桌会议“)&amp;”

     

&amp; accessoriesTables =“&amp; Server.URLEncode(”Buffet,Cake,Gift,Beverage Station“)&amp;”

     

&安培; accessoriesEntertainment =

     

&amp; estimatedArea =“&amp; Server.URLEncode(”1200 sq ft“)&amp;”

     

&amp; estimatedTentSize =“&amp; Server.URLEncode(”30 ft x 40 ft or 20 ft x 60 ft“)&amp;”

     

&amp; estimatedRoomToSpare =“&amp; Server.URLEncode(”0 sq ft or 0 sq ft“)&amp;”

     

&amp; contactName =“&amp; Server.URLEncode(”Jonathan Chan“)&amp;”

     

&安培; contactPhone = 9011234567

     

&amp; contactEmail =“&amp; Server.URLEncode(”jchan@lunaweb.com“)&amp;”

     

&amp; contactComments =“&amp; Server.URLEncode(”这是一个很长的评论。“)

     

在我的ASP示例中,您可能已经注意到围绕字符串的“Server.URLEncode”。这是>因此可能弄乱数据的某些字符被编码为%十六进制ASCII&gt;值。例如,如果有人输入“我爱奶酪&amp; amp;蛋糕“作为他的评论,程序&gt;会认为&符号表示新的键/值对。如果我们对网址进行编码,则&gt;将显示为“I%20love%20cheese%20%26%20cake”。

2 个答案:

答案 0 :(得分:2)

  1. 查看logcat文件中的错误
  2. 检查您是否在清单文件中包含了Internet权限。

答案 1 :(得分:0)

永远不要这样做

} catch (ClientProtocolException e) {
     // TODO Auto-generated catch block
} catch (IOException e) {
     // TODO Auto-generated catch block
}

点击此处查看如何正确执行:http://source.android.com/source/code-style.html

相关问题