http post method problem
In my application I'm using outpost method to send data to web service which is in .net.Now when i send string with spaces or special characters it takes the string with e.g. test string would display like test+string.
I'm using httpDefaultClient with nameValuePair to send data...i've used UrlEncode function to encode my string but still result is the same... Please help me...
Here is my code
//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.printSta开发者_StackOverflow社区ckTrace();
String message=e.getMessage();
Log.e("meaasge with erroe",message);
}
return Responce;
}
step 1
You have specified a "UTF-8" encoding in
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
Try to change it to
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs);
step 2
I noticed that the strings you are adding to the request are called encodeXXXX
Does this mean that you are encoding them before adding them to the ValuePairs?
If so, stop doing this and keep them as normal strings.
精彩评论