HttpResponse状态码400 android

时间:2014-05-12 07:36:42

标签: android httpresponse http-status-code-400

我在登录时向服务器发送GET请求。我希望 200响应,但我登录时会获得 400状态代码。 稍后,如果我退出应用并重新启动它,我会收到200状态代码。

我想做什么:

  • 我登录了一个网站。
  • 我得到一个deviceId,EncodedAccountName和hostUrl作为回应。
  • 我被重定向到另一个url,其中包含url中的这些参数。然后我将重定向的url与字符串匹配,如果匹配,则停止webview加载。
  • 当重定向的网址与我的字符串匹配时,我应该从响应中获取其他参数以及200状态代码,我将获得400。

我对此行为感到困惑:

这是我的代码:

Login.java

myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon)  {
        super.onPageStarted(view, url, favicon);
        loadingFinished = false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if(!isRedirected) {
            loadingFinished = true;
        }

        if(progressBar!=null && progressBar.isShowing()) {
            progressBar.dismiss();
        }
        url= view.getUrl();
        redirected= Uri.decode(url);

        if(loadingFinished && ! isRedirected && redirected!=null && redirected.contains("redirect_uri="+endpointHost+"/AuthorizeDevice/index/"+deviceId+"?{StandardTokens}")) {
            Utility.ProgressDialogFragment progDialog = new Utility.ProgressDialogFragment();
            progDialog.show(getFragmentManager(), "Wait");
            myWebView.stopLoading();

            //String authorizationContentString =  AuthenticateDeviceEndPoint+ "?encodedAccountName=" + encodedAccountName + "&deviceId=" + deviceId + "&hostUrl=" + hostUrl;
            // authorizationContentString = authorizationContentString.replace("|", "%7C");

            //UserInformation userInfo;
            JSONParser parser = new JSONParser();
            Object register;
            try  {
                // userInfo 
                // String responseData= new AuthenticateDevice(Details1.this,myprogressDialog).execute(authorizationContentString).get();
                String responseData = new AuthenticateDevice(Details1.this, progDialog, encodedAccountName, deviceId, hostUrl).execute(AuthenticateDeviceEndPoint).get();
                register = parser.parse(responseData);
            }

AuthenticateDevice.java

public class AuthenticateDevice extends AsyncTask<String, String, String> {

    public Context mContext;
    String responseBody = "" ;
    ProgressDialog dialog;
    Utility.ProgressDialogFragment progDia;
    FragmentManager man;
    String encodedAccName, deviceId, hostUrl;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        /* progDia= new Utility.ProgressDialogFragment().newInstance();
        progDia.show(man, "wait");
        dialog=new ProgressDialog(mContext);
        dialog.setTitle("Processing..");
        dialog.setMessage("Please wait..");
        dialog.show();*/
    }

    public AuthenticateDevice(Details1 myContext, ProgressDialogFragment progDialog, String encodedAccountName, String deviceId, String hostUrl) {
        mContext = myContext;
        this.progDia = progDialog;
        this.encodedAccName = encodedAccountName;
        this.deviceId = deviceId;
       this.hostUrl = hostUrl;
    }

    @Override
    protected String doInBackground(String... params) {
        String Url = params[0];
        if(!Url.endsWith("?"))
            Url += "?";
        List<NameValuePair> nameValuePair = new LinkedList<NameValuePair>();
        nameValuePair.add(new BasicNameValuePair("encodedAccountName", encodedAccName));
        nameValuePair.add(new BasicNameValuePair("deviceId", deviceId));
        nameValuePair.add(new BasicNameValuePair("hostUrl", hostUrl));
        String paramString = URLEncodedUtils.format(nameValuePair, "utf-8");
        Url += paramString;

        HttpResponse response =null;
        String resultString = "";

        // Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpGet request = new HttpGet(Url);

        try {

            response = httpClient.execute(request);
            if(response.getStatusLine().getStatusCode()== 200) {
                HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        InputStream inputStream = entity.getContent();
                        responseBody = convertToString(inputStream);
                        return responseBody;
                    }
                }

            } catch (ClientProtocolException e)  {
                e.printStackTrace();
            } catch (IOException e)  {
                e.printStackTrace();
            }
        return  responseBody;
    }
}

0 个答案:

没有答案