0

I'm trying to post some data to my server(Strings). I'm getting a missing parameter error but i'm not sure why. I log out the contents of the NameValuePair and the data seems to be there.

I'm running this through the emulator. Are there any ideas why the server would reurn such an error if the params are there?

public String updateCall( String userID, String callID, String startTime, String duration, String carerID) {

        Log.e(TAG,"inside WS updateCall");

        DateTime now = new DateTime();
        DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
        String formattedNowTime = df.print(now);


        List<NameValuePair> nameValuePairs = null;
        String serviceStatus = null;
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://rr3test.carefree-app.co.uk/roadrunner.asmx/UpdateCall");


        userID = userID.trim();
        formattedNowTime = formattedNowTime.trim();
        callID = callID.trim();
        startTime = startTime.trim();
        duration = duration.trim();
        carerID = carerID.trim();



        try {
            // Add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("UserID", userID));
            nameValuePairs.add(new BasicNameValuePair("DateTime", formattedNowTime));
            nameValuePairs.add(new BasicNameValuePair("CallID", callID));
            nameValuePairs.add(new BasicNameValuePair("Start", startTime));
            nameValuePairs.add(new BasicNameValuePair("Duration", duration));
            nameValuePairs.add(new BasicNameValuePair("NewCarerID", carerID));

            Log.e(TAG, "userID = " + userID + " DateTime = " + formattedNowTime + " callID = " + callID + " startTime = " + startTime 
                                                                        + " duration = " + duration + " carerID = " + carerID);

            Log.e(TAG, "nameValuePair length = " + nameValuePairs.size());

            for(int i = 0; i < nameValuePairs.size(); i++){

                Log.e(TAG, "nameValuePairs" + nameValuePairs.get(i));

            }



            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


            Log.e(TAG, "httppost.getEntity() = " + httppost.getEntity().getContentLength());

            // Execute HTTP Post Request
            Log.e(TAG, "about to execute http post updateCall");
            HttpResponse response = httpclient.execute(httppost);


            Log.e(TAG, "just executed http post updateCall");
            String responseBody = EntityUtils.toString(response.getEntity());
            Log.e(TAG, "responseBody = " + responseBody);

            DocObjModParser dp = new DocObjModParser(context);
            serviceStatus = dp.getPostTransactionResponseCode(responseBody);

            Log.e(TAG, "serviceStatus = " + serviceStatus);




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

            nameValuePairs.clear();
            nameValuePairs = null;

        }
        return serviceStatus;




    }//end of postData

.

02-17 17:14:15.268: E/WebService(500): inside WS updateCall
02-17 17:14:15.418: E/WebService(500): userID = 1 DateTime = 2014-02-17 17:14:15.307 callID = 464da2e9-4d48-4b7c-a134-3180ce0ef250 startTime = 17-Feb-2014 5:14 duration = 30 carerID = 761
02-17 17:14:15.458: E/WebService(500): nameValuePair length = 6
02-17 17:14:15.458: E/WebService(500): nameValuePairsUserID=1
02-17 17:14:15.458: E/WebService(500): nameValuePairsDateTime=2014-02-17 17:14:15.307
02-17 17:14:15.458: E/WebService(500): nameValuePairsCallID=464da2e9-4d48-4b7c-a134-3180ce0ef250
02-17 17:14:15.458: E/WebService(500): nameValuePairsStart=17-Feb-2014 5:14
02-17 17:14:15.458: E/WebService(500): nameValuePairsDuration=30
02-17 17:14:15.458: E/WebService(500): nameValuePairsNewCarerID=761
02-17 17:14:15.538: E/WebService(500): httppost.getEntity() = 141
02-17 17:14:15.538: E/WebService(500): about to execute http post updateCall
02-17 17:14:15.728: D/dalvikvm(500): GC_FOR_ALLOC freed 487K, 16% free 8965K/10631K, paused 98ms
02-17 17:14:15.728: I/dalvikvm-heap(500): Grow heap (frag case) to 8.926MB for 87396-byte allocation
02-17 17:14:15.828: D/dalvikvm(500): GC_FOR_ALLOC freed 0K, 16% free 9050K/10759K, paused 96ms
02-17 17:14:15.938: D/dalvikvm(500): GC_FOR_ALLOC freed 0K, 16% free 9050K/10759K, paused 97ms
02-17 17:14:15.938: I/dalvikvm-heap(500): Grow heap (frag case) to 9.009MB for 87396-byte allocation
02-17 17:14:16.048: D/dalvikvm(500): GC_FOR_ALLOC freed 0K, 17% free 9135K/10887K, paused 101ms
02-17 17:14:16.728: D/dalvikvm(500): GC_FOR_ALLOC freed 177K, 16% free 9237K/10887K, paused 95ms
02-17 17:14:16.741: I/dalvikvm-heap(500): Grow heap (frag case) to 9.192MB for 87396-byte allocation
02-17 17:14:16.838: D/dalvikvm(500): GC_FOR_ALLOC freed 0K, 16% free 9322K/11015K, paused 99ms
02-17 17:14:16.928: E/WebService(500): just executed http post updateCall
02-17 17:14:16.958: E/WebService(500): responseBody = Missing parameter: UserID.
02-17 17:14:16.958: E/DocObjModParser(500): inside domparser constructor
02-17 17:14:16.958: E/DocObjModParser(500): built the dom factory
02-17 17:14:16.968: W/dalvikvm(500): threadid=12: thread exiting with uncaught exception (group=0x40014760)
02-17 17:14:16.968: E/AndroidRuntime(500): FATAL EXCEPTION: AsyncTask #4
02-17 17:14:16.968: E/AndroidRuntime(500): java.lang.RuntimeException: An error occured while executing doInBackground()
02-17 17:14:16.968: E/AndroidRuntime(500):  at android.os.AsyncTask$3.done(AsyncTask.java:266)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.lang.Thread.run(Thread.java:1020)
02-17 17:14:16.968: E/AndroidRuntime(500): Caused by: java.lang.NullPointerException
02-17 17:14:16.968: E/AndroidRuntime(500):  at com.carefreegroup.rr3.carefreeoncall.DocObjModParser.getPostTransactionResponseCode(DocObjModParser.java:505)
02-17 17:14:16.968: E/AndroidRuntime(500):  at com.carefreegroup.rr3.carefreeoncall.WebService.updateCall(WebService.java:352)
02-17 17:14:16.968: E/AndroidRuntime(500):  at com.carefreegroup.rr3.carefreeoncall.UpdateCallDateTimeAndCarerActivity$AsyncUpDateCall.doInBackground(UpdateCallDateTimeAndCarerActivity.java:205)
02-17 17:14:16.968: E/AndroidRuntime(500):  at com.carefreegroup.rr3.carefreeoncall.UpdateCallDateTimeAndCarerActivity$AsyncUpDateCall.doInBackground(UpdateCallDateTimeAndCarerActivity.java:1)
02-17 17:14:16.968: E/AndroidRuntime(500):  at android.os.AsyncTask$2.call(AsyncTask.java:252)
02-17 17:14:16.968: E/AndroidRuntime(500):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-17 17:14:16.968: E/AndroidRuntime(500):  ... 4 more
02-17 17:14:19.288: E/WindowManager(500): Activity com.carefreegroup.rr3.carefreeoncall.UpdateCallDateTimeAndCarerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4079a3e8 that was originally added here
02-17 17:14:19.288: E/WindowManager(500): android.view.WindowLeaked: Activity com.carefreegroup.rr3.carefreeoncall.UpdateCallDateTimeAndCarerActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4079a3e8 that was originally added here
02-17 17:14:19.288: E/WindowManager(500):   at android.view.ViewRoot.<init>(ViewRoot.java:276)
02-17 17:14:19.288: E/WindowManager(500):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:152)
02-17 17:14:19.288: E/WindowManager(500):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
02-17 17:14:19.288: E/WindowManager(500):   at android.view.Window$LocalWindowManager.addView(Window.java:514)
02-17 17:14:19.288: E/WindowManager(500):   at android.app.Dialog.show(Dialog.java:269)
02-17 17:14:19.288: E/WindowManager(500):   at com.carefreegroup.rr3.carefreeoncall.UpdateCallDateTimeAndCarerActivity$AsyncUpDateCall.onPreExecute(UpdateCallDateTimeAndCarerActivity.java:198)
02-17 17:14:19.288: E/WindowManager(500):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
02-17 17:14:19.288: E/WindowManager(500):   at android.os.AsyncTask.execute(AsyncTask.java:499)
02-17 17:14:19.288: E/WindowManager(500):   at com.carefreegroup.rr3.carefreeoncall.UpdateCallDateTimeAndCarerActivity$1.onClick(UpdateCallDateTimeAndCarerActivity.java:117)
02-17 17:14:19.288: E/WindowManager(500):   at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-17 17:14:19.288: E/WindowManager(500):   at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 17:14:19.288: E/WindowManager(500):   at android.os.Looper.loop(Looper.java:126)
02-17 17:14:19.288: E/WindowManager(500):   at android.app.ActivityThread.main(ActivityThread.java:3997)
02-17 17:14:19.288: E/WindowManager(500):   at java.lang.reflect.Method.invokeNative(Native Method)
02-17 17:14:19.288: E/WindowManager(500):   at java.lang.reflect.Method.invoke(Method.java:491)
02-17 17:14:19.288: E/WindowManager(500):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-17 17:14:19.288: E/WindowManager(500):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-17 17:14:19.288: E/WindowManager(500):   at dalvik.system.NativeStart.main(Native Method)

.

private class AsyncUpDateCall extends AsyncTask<Void, Void, Void> {

        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {



            progressDialog = new ProgressDialog(UpdateCallDateTimeAndCarerActivity.this);
            progressDialog.setTitle("Connecting to Server");
            progressDialog.setMessage("updating call...");
            progressDialog.setIndeterminate(true);
            progressDialog.show();

        };

        @Override
        protected Void doInBackground(Void... params) {

            rrOnCallApp.webService.updateCall("1", callID, startTime, duration,
                    theCarerID);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            if (progressDialog != null)
                progressDialog.dismiss();
        }

    }
turtleboy
  • 7,612
  • 27
  • 93
  • 187
  • Post your AsyncTask class. – Piyush Feb 18 '14 at 09:24
  • @PiyushGupta hi i've posted the Async. I know it only has 5 params instead of 6 but the 6th one i create in the web method – turtleboy Feb 18 '14 at 09:29
  • change this one... from nameValuePairs = new ArrayList(2); to nameValuePairs = new ArrayList(); – Piyush Feb 18 '14 at 09:33
  • This is because if you have given the length of 2 it means that you can post 2 item with nameValuePairs. So change it. – Piyush Feb 18 '14 at 09:36
  • @PiyushGupta no it makes no difference that. I've sent data before using new ArrayList(2) and there has been 9 Strings sent. I'm not sure if the 2 refers to how many things can be sent? – turtleboy Feb 18 '14 at 09:41

2 Answers2

1

You get

E/WebService(500): responseBody = Missing parameter: UserID.

while you try

        Log.e(TAG, "responseBody = " + responseBody);

        DocObjModParser dp = new DocObjModParser(context);
        serviceStatus = dp.getPostTransactionResponseCode(responseBody); // <== this fails!

No surprise, DocObjModParser cannot parse the response string "Missing parameter: UserID."

The response suggests adding the user id to the http query.

I'd suggest printing the httppost and trying to perform it via, at first, a browser (it may give you a hint immediately), and, at second, via curl, a command-line utility available in Linux or Cygwin (not sure about plain Windows).

Another suggestion is to compare the printed httppost with a request that works. Who knows, maybe the script is sensitive to the order of parameters or some string contains characters that should be URL-encoded.

UPD: the problem may be with the headers

curl -I https://rr3test.carefree-app.co.uk/roadrunner.asmx/UpdateCallTest
HTTP/1.0 200 Connection established

HTTP/1.1 500 Internal Server Error
Connection: Keep-Alive
Content-Length: 3026
Date: Tue, 18 Feb 2014 11:54:29 GMT
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
Cache-Control: private
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET

UPD2:

$ curl -I -X GET -H "Accept: text/javascript, text/html, application/xml, */*" https://rr3test.carefree-app.co.uk/roadrunner.asmx/UpdateCallTest?UserID=asd
HTTP/1.0 200 Connection established

HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: 95
Date: Tue, 18 Feb 2014 12:00:22 GMT
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
Cache-Control: private, max-age=0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET


$ curl   --data "UserID=asd" -H "Accept: text/javascript, text/html, application/xml, */*" https://rr3test.carefree-app.co.uk/roadrunner.asmx/UpdateCallTest
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">UserID=asd</string>
18446744073709551615
  • 14,600
  • 3
  • 82
  • 116
  • Doesn't NameValuePair add the UserID to the query via setEntity? – turtleboy Feb 18 '14 at 09:45
  • looks like. try to print the request URL – 18446744073709551615 Feb 18 '14 at 09:52
  • ok thanks, how can i print the httpPost? The android api has a few methods like httppost.getEntity, httppost.getHeaders etc but i'm not sure which method will give me the url with params that i send to the server. – turtleboy Feb 18 '14 at 10:27
  • Try http://developer.android.com/reference/org/apache/http/client/methods/HttpUriRequest.html#getURI() _getURI()_ or http://developer.android.com/reference/org/apache/http/HttpRequest.html#getRequestLine() _getRequestLine()_ – 18446744073709551615 Feb 18 '14 at 10:36
  • ok i have used the following after setting the Entity on the httppost obj. RequestLine req = httppost.getRequestLine(); it returns the following 02-18 10:50:59.988: E/WebService(956): req = POST http://rr3test.carefree-app.co.uk/roadrunner.asmx/UpdateCallTest HTTP/1.1 – turtleboy Feb 18 '14 at 10:54
  • I have just tried your URL in a browser: it shows _Missing parameter: UserID._ while https://rr3test.carefree-app.co.uk/roadrunner.asmx/UpdateCallTest&UserID=asd shows _Bad Request_. Are you sure you should use POST rather than GET? – 18446744073709551615 Feb 18 '14 at 11:00
  • well i think this is the problem. The response from httppost.getRequestLine isn't showing any params, when i am thinking i am adding them in the setEntity. Shouldn't getRequestLine show the params aswell? – turtleboy Feb 18 '14 at 11:08
  • http://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request == How are parameters sent in an HTTP POST request? – 18446744073709551615 Feb 18 '14 at 11:24
  • ah i remember now, they are sent in the body. Is there a way to examine them, i can't see any method in hte API that gets the body or is it a case of using something like fiddler? – turtleboy Feb 18 '14 at 11:32
  • i just want to see if the params are there or not, how best do that? – turtleboy Feb 18 '14 at 11:32
  • can you print the request on the server side? – 18446744073709551615 Feb 18 '14 at 11:42
  • http://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request – 18446744073709551615 Feb 18 '14 at 11:43
  • Thanks for the update. I've changed how i make the request from a POST to a GET and it works now. I need to explain this to the other developer that does the server-side code. If i want to make a POST what does he need to do on the server to accept this way of doing it? – turtleboy Feb 18 '14 at 12:09
  • I think that developer may ask a question with the _asp.net_ tag here on stackoverflow, if necessary. You may try to search for "ASP.NET http server tutorial post get", the information will most likely pop up immediately. – 18446744073709551615 Feb 18 '14 at 12:29
1

In seems that the client successfully sent the request to server, but the server response that parameter UserID not found. Please check the server code, one reason may be the server handle parameter with the GET method instead of POST (what you are using to send data now)

VinhNT
  • 1,043
  • 7
  • 13
  • Hi It works when i do a GET instead of a POST. I don't write the server-side code. What has the other developer done or what does he have to do to make this work with a POST? Thanks – turtleboy Feb 18 '14 at 12:22
  • 1
    It quite easy to change the way to handle the request from POST to GET, depend on the language the server used. For example, with php, instead of using the $_GET["Parameter_Name"], the developer can use $_POST["Parameter_Name"] – VinhNT Feb 19 '14 at 04:27