0

Im trying to post to a REST API json object but i keep getting responce 404 but the url is working fine. Can anyone tell me why is this happeninig?

Here is my code:

new AsyncTask<Void, Void, Void>() {

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

                HttpPost request = new HttpPost(
                        "http://grpsvil-webservice.si2001.it/RestChannelApp.svc/CheckCoupon");
                // request.setHeader("Accept", "application/json");
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-type", "application/json");

                try {
                    // Build JSON string
                    JSONStringer vehicle = new JSONStringer()
                            .object()
                            .key("CouponVerificationCode")
                            .value("adf")
                            .key("ApiKey")
                            .value("adfadf123")
                            .key("Token")
                            .value("fgsg342==")
                            .endObject();
                    Log.v("--", vehicle.toString());
                    StringEntity entity = new StringEntity(vehicle.toString());

                    request.setEntity(entity);

                    // Send request to WCF service
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpResponse response = httpClient.execute(request);
                    int resCode = response.getStatusLine().getStatusCode();
                    Log.v("--", response.getStatusLine().getStatusCode() + "");

                    if (resCode == 200) {

                        Toast.makeText(getApplicationContext(),
                                response.getStatusLine().getStatusCode() + "",
                                Toast.LENGTH_LONG).show();
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(response.getEntity()
                                        .getContent()));
                        String line = "";
                        StringBuffer returnFromServer = new StringBuffer();

                        while ((line = in.readLine()) != null) {
                            returnFromServer.append(line);
                        }
                        // Toast what we got from server
                        Log.v("--", "!@# " + returnFromServer.toString());

                        if (entity != null) {
                            entity.consumeContent();
                        }

                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
                Intent i = new Intent(Splash.this, Login.class);
                startActivity(i);
                finish();
                return null;
            }
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117

4 Answers4

1

Endpoint not found.

Probably you write wrong methode url for this api

Lucian Novac
  • 1,255
  • 12
  • 18
1

Try sending the verification code like this

ArrayList<NameValuePair> parms = new ArrayList<NameValuePair>();
parms.add(new BasicNameValuePair("CouponVerificationCode", adf));
parms.add(new BasicNameValuePair("ApiKey", adfadf123));
parms.add(new BasicNameValuePair("Token", fgsg342==));

httppost.setEntity(new UrlEncodedFormEntity(parms));

So your overall code will be

HttpPost request = new HttpPost(
                    "http://grpsvil-webservice.si2001.it/RestChannelApp.svc/CheckCoupon");
            // request.setHeader("Accept", "application/json");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");

            try {
                ArrayList<NameValuePair> parms = new ArrayList<NameValuePair>();
                parms.add(new BasicNameValuePair("CouponVerificationCode", adf));
                parms.add(new BasicNameValuePair("ApiKey", adfadf123));
                parms.add(new BasicNameValuePair("Token", fgsg342==));
                request.setEntity(new UrlEncodedFormEntity(parms));
                request.setEntity(entity);

                // Send request to WCF service
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpResponse response = httpClient.execute(request);

                int resCode = response.getStatusLine().getStatusCode();
                Log.v("--", response.getStatusLine().getStatusCode() + "");
Mansuu....
  • 1,206
  • 14
  • 27
MDMalik
  • 3,951
  • 2
  • 25
  • 39
1

If you get all the WSDL from http://grpsvil-webservice.si2001.it/RestChannelApp.svc?WSDL you can see all the operations that you have defined.

CheckCoupon is not there, BUT there's a CheckPromotionalCode.

Could be that one?

Enrichman
  • 11,157
  • 11
  • 67
  • 101
1

The URL returns 404 not found. Just because it shows some fancy error text in the browser, doesn't mean the status code is 200 OK. This is the HTTP response that I get:

Status Code: 404 Not Found
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
Age: 0
Cache-Control: private
Connection: Keep-Alive
Content-Length: 1565
Content-Type: text/html; charset=UTF-8
Date: Thu, 27 Feb 2014 14:58:03 GMT
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
access-control-allow-origin: *
x-powered-by: ASP.NET
Mark Buikema
  • 2,483
  • 30
  • 53