来自android应用服务调用的错误http响应

时间:2019-06-19 08:48:24

标签: java android json rest http

当我从高级Rest客户端从服务(以下代码中的anagrafica)调用REST api时,它将返回带有预期结果的HTTP响应代码200。但是,当我从Android应用程序通过具有相同负载的相同服务调用它时,它将返回HTTP响应代码302。怎么可能?

两个用于说明问题的屏幕截图:

  1. api call from advanced rest console
  2. same api call from android app

正在从应用程序中调用服务:

        try {
            String ex = extractUni(getApplicationContext());
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("username", this.username));
            nameValuePairs.add(new BasicNameValuePair("password", this.password));
            URL address = new URL("https://api.xxx.xx/"+ex+"/anagrafica/");
            URLDataReader pp = new URLDataReader(getApplicationContext(), this, nameValuePairs);
            pp.execute(address);
        } catch (Exception e) {
            e.printStackTrace();
            CommonUtils cu = new CommonUtils();
            cu.sendReport(e, getApplicationContext());
        }

这是管理呼叫的类:

    public class URLDataReader extends AsyncTask<URL, String, String> {

        Context mContext;
        OnAsyncTaskComplete listener;
        List<NameValuePair> nameValuePairs;
        public URLDataReader asyncObject;
        CountDownTimer cc;
        String url = "";
    public URLDataReader(Context context, OnAsyncTaskComplete lisener, List<NameValuePair> nameValuePairs) {
        mContext = context;
        listener = lisener;
        this.nameValuePairs = nameValuePairs;
    }

    @Override
    protected String doInBackground(URL... data) {


        HttpClient HttpUrlConnection  = new DefaultHttpClient();
        HttpParams httpParameters = HttpUrlConnection.getParams();

        HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
        HttpConnectionParams.setSoTimeout(httpParameters, 15000);
        HttpConnectionParams.setTcpNoDelay(httpParameters, true);

        HttpPost httppost = new HttpPost(data[0].toString());

        //this.url = httppost.toString();

        HttpResponse response = null;

        System.setProperty("http.keepAlive", "false");
        System.setProperty("java.net.preferIPv4Stack" , "true");

        try {

            httppost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs));
            //SETTA I POST IN GLOBALS
            response = HttpUrlConnection.execute(httppost);


        } catch (Exception e) {

            e.printStackTrace();
            CommonUtils cu = new CommonUtils();
            cu.sendReport(e, mContext);

        }

        try {
            return convertHttpResponseToString(response);
        } catch (Exception e) {

            e.printStackTrace();
            CommonUtils cu = new CommonUtils();
            cu.sendReport(e, mContext);

        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Toast.makeText(mContext, "start timeout", Toast.LENGTH_LONG).show();
        asyncObject = this;

        cc = new CountDownTimer(10000, 1000) {

            public void onTick(long millisUntilFinished) {
                int seconds = (int) (millisUntilFinished / 1000);
                //Toast.makeText(mContext, seconds+"", Toast.LENGTH_LONG).show();
            }

            public void onFinish() {
                // stop async task if not in progress
                if (asyncObject.getStatus() == Status.RUNNING ||
                        asyncObject.getStatus() == Status.PENDING) {
                    asyncObject.cancel(true);
                    cc.cancel();
                    listener.OnAsyncTaskTimeout();
                    //Toast.makeText(mContext, "finish", Toast.LENGTH_LONG).show();
                }
            }

        }.start();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        cc.cancel();
        listener.OnAsyncTaskComplete(s);
    }
    private String convertHttpResponseToString(HttpResponse response) throws IOException {
        InputStream responseStream = response.getEntity().getContent();
        Scanner scanner = new Scanner(responseStream, "UTF-8");
        String responseString = scanner.useDelimiter("\\Z").next();
        scanner.close();
        return responseString;
    }
}

0 个答案:

没有答案