0

我试图发布到 REST API json 对象,但我不断收到响应 404,但 url 工作正常。谁能告诉我为什么会这样?

这是我的代码:

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;
            }
4

4 回答 4

1

如果您从http://grpsvil-webservice.si2001.it/RestChannelApp.svc?WSDL获取所有 WSDL,您可以看到您定义的所有操作。

CheckCoupon 不存在,但有一个CheckPromotionalCode

会是那个吗?

于 2014-02-27T14:59:23.160 回答
1

URL 返回 404 未找到。仅仅因为它在浏览器中显示一些花哨的错误文本,并不意味着状态码是 200 OK。这是我得到的 HTTP 响应:

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
于 2014-02-27T14:59:26.940 回答
1

未找到端点。

可能你为这个 api 写了错误的方法 URL

于 2014-02-27T14:40:59.393 回答
1

尝试像这样发送验证码

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));

所以你的整体代码将是

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() + "");
于 2014-02-27T14:43:28.817 回答