HTTPPOST返回404 ANDROID

时间:2014-02-27 14:34:44

标签: java android androidhttpclient android-json

我试图发布到REST API json对象,但我一直得到响应404但网址工作正常。谁能告诉我为什么会发生这种情况?

这是我的代码:

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 个答案:

答案 0 :(得分:1)

未找到端点。

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

答案 1 :(得分: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() + "");

答案 2 :(得分:1)

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

CheckCoupon不存在,但有一个CheckPromotionalCode

可能是那个吗?

答案 3 :(得分:1)

网址返回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