views:

138

answers:

1

I've been working on an app that uses both GET and POST requests to Web Services. The GET requests are no problems but the POST requests are killing me. I've tried 2 different scenarios in the code. The first looks like this...

 HttpClient httpclient = new DefaultHttpClient();  
 HttpPost httppost = new HttpPost(ws);  
 JSONObject jsonObject = new JSONObject();

 try {  
// Add your data  
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
jsonObject.put("action", "login");
jsonObject.put("username", "*********");
    jsonObject.put("password", "*********");

    httppost.setHeader("jsonString", jsonObject.toString());  
StringEntity se = new StringEntity( jsonObject.toString());    
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));  
httppost.setEntity(se); 


            // Execute HTTP Post Request  
            HttpResponse response = httpclient.execute(httppost);  
            textview.setText(getResponse(response.getEntity()));
        } catch (ClientProtocolException e) {  
            textview.setText(e.getLocalizedMessage());
        } catch (IOException e) {  
            textview.setText(e.getLocalizedMessage());
        } catch (JSONException e) {
            textview.setText(e.getLocalizedMessage());
        }

This code gets this result for me...
"Bad Request (Invalid Header Name)"

Now here's my second piece of code...

 HttpClient httpclient = new DefaultHttpClient();  
      HttpPost httppost = new HttpPost(ws);  
      JSONObject jsonObject = new JSONObject();

        try {  
            // Add your data  
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
            jsonObject.put("action", "login");
            jsonObject.put("username", "******");
            jsonObject.put("password", "******");

            nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

            // Execute HTTP Post Request  
            HttpResponse response = httpclient.execute(httppost);  

            textview.setText(getResponse(response.getEntity()));
        } catch (ClientProtocolException e) {  
            textview.setText(e.getLocalizedMessage());
        } catch (IOException e) {   
            textview.setText(e.getLocalizedMessage());
        } catch (JSONException e) {

        }

This gives me an entirely different result. It's a long garbled mess of xml and SOAP that does have a SOAP Exception mentioned in it...
"Server was unable to process request. --- System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1."
Now, can anyone shed any light on what I am doing wrong.

+2  A: 

in your second piece of code add ::

   // Execute HTTP Post Request  
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
    httppost.setEntity(formEntity);
    HttpResponse response = httpclient.execute(httppost);   
Jorgesys
Adding that bit in got me the same XML SOAP error.
huffmaster