http post方法问题

时间:2011-08-05 12:24:26

标签: android

在我的应用程序中,我使用outpost方法将数据发送到.net.Now中的Web服务。当我发送带有空格或特殊字符的字符串时,它将带有例如字符串的字符串。测试字符串将显示为test + string。

我正在使用带有nameValuePair的httpDefaultClient来发送数据...我使用了UrlEncode函数对我的字符串进行编码,但结果仍然相同...... 请帮帮我......

这是我的代码

//web service call
                HttpClient client=new DefaultHttpClient();
                HttpPost request = new HttpPost();
                request.setURI(new URI(url2));

                List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
                //  nameValuePairs.add(new BasicNameValuePair("CreateHotSpots", value))
                nameValuePairs.add(new BasicNameValuePair("sKey",""+globalClass.getUser_key()));
                nameValuePairs.add(new BasicNameValuePair("sLAKE",encodedlakename));
                if(!(DragAndDropPinActivity.point.isEmpty())){
                    nameValuePairs.add(new BasicNameValuePair("sLAT",""+DragAndDropPinActivity.point.get(0)));
                    nameValuePairs.add(new BasicNameValuePair("sLon",""+DragAndDropPinActivity.point.get(1)));

                }
                else
                {
                    url2=url2.concat("&sLAT="+""+myLatitude);
                    url2=url2.concat("&sLon="+""+myLongitude);

                }

                nameValuePairs.add(new BasicNameValuePair("sDesc",encodedDesc));
                nameValuePairs.add(new BasicNameValuePair("sSpeciesofFish",encodedFishSpecies));
                nameValuePairs.add(new BasicNameValuePair("sBaitUsed",encodedbUsed));
                nameValuePairs.add(new BasicNameValuePair("sWeatherInformation",encodedwInfo));
                nameValuePairs.add(new BasicNameValuePair("season",encodedlseason));
                nameValuePairs.add(new BasicNameValuePair("sDesc",""+encodedDesc));
                if(share)
                {
                    nameValuePairs.add(new BasicNameValuePair("sShareInfo","true"));
                }
                else
                {
                    nameValuePairs.add(new BasicNameValuePair("sShareInfo","false"));
                }

                Log.e("name value pairs",""+nameValuePairs.toString());
                UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
                request.setEntity(entity_st);

                HttpResponse response = client.execute(request);
                HttpEntity resEntity = response.getEntity();  
                if (resEntity != null) { 

                    Responce=EntityUtils.toString(resEntity);
                    Log.i("responce ======",""+Responce);

                }

            }
            catch (Exception e) {
                e.printStackTrace();
                String message=e.getMessage();
                Log.e("meaasge with erroe",message);
            }

            return Responce;
        }

1 个答案:

答案 0 :(得分:2)

第1步

您已在

中指定了“UTF-8”编码
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8"); 

尝试将其更改为

UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs); 

第2步

我注意到您添加到请求中的字符串称为encodeXXXX

这是否意味着您在将它们添加到ValuePairs之前对它们进行编码?

如果是这样,请停止此操作并将其保留为普通字符串。