Pergunta

I have a webservice which receives 2 (optional) parameters:

<resource path="getLogbookEvents">
   <method name="POST">
     <request>
        <param name="startDate" style="query" type="xs:string"/>
        <param name="endDate" style="query" type="xs:string"/>
     </request>
     <response>
        <representation mediaType="application/json"/>
     </response>
   </method>
</resource>

I'm able to connect and receive an answer from Android using HttpPost, but the web service never receives either of the two parameters.

HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://... method URL ...");
//post.setHeader("media-type", "application/json; charset=UTF-8");

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate", "2014-04-18T05:00:00"));
nameValuePairs.add(new BasicNameValuePair("endDate", "2014-04-18T06:00:00"));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);

// entity.setContentEncoding(HTTP.UTF_8);
post.setEntity(entity);

// make POST request to the given URL
HttpResponse httpResponse = httpclient.execute(post);

// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();

I have tried HttpParams, JSONObject and nameValuePairs and nothing works. I just keep receiving the response as if I didn't specify any parameters. Any ideas on why this could be happening or other things I could try to get the winning combination?

Foi útil?

Solução 2

I finally managed to get it working.

So, first of all, there's definitely something wrong with the webservice. I'm not exactly sure what, and it's not my business to fix it, so I'm not even going to try to pinpoint what it was.

Basically, the getLogbookEvents wasn't receiving parameters because it won't accept any parameters inside the body of the request, period. Looking at the SoapUI raw data being sent I discovered that it was always sending the parameters inside the URL, not the body ...

I have no idea how they got a POST method to behave like a GET method, but as it stands they apparently do. Formatting my nameValuePairs in UTF-8 and adding them to the URL is the (totally backwards and not recommended at all) way I got it to work :)

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate", "2014-04-18T05:00:00"));
nameValuePairs.add(new BasicNameValuePair("endDate", "2014-04-18T06:00:00"));

String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
HttpPost post = new HttpPost("... basic URL String ..." + paramString);

Outras dicas

not a direct answer, never used httpclient directly but to debug/fix this issue you could:

  • use proxy and see what data is really being sent from your phone
  • use easy to use http libs such as http-request
  • or retrofit

go for http-request. it is dead simple to use. trust me.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top