0

I am sending a GET request to the server when I login. I am expecting a 200 response, but I get a 400 status code when I login. Later if I exit the app and relaunch it, I get a 200 status code.

What I want to do:

  • I login to a website.
  • I get a deviceId, EncodedAccountName and hostUrl in response.
  • I get redirected to another url which consists of these parameters in the url. I then match the redirected url with a string and if it matches I stop the webview loading.
  • When the redirected url matches my string I am supposed to get other parameters from the response along with a 200 status code where I get a 400 instead.

Iam confused with this behaviour:

Here is my code:

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;
    }
}
tanou
  • 1,083
  • 2
  • 13
  • 33
  • A 400 code is a "bad request syntax" error, so have you check your request format and are you sure that it's well formed? Is there a difference in the parameters you're passing between the "200" response and the "400" response? – tanou May 12 '14 at 08:00
  • @tanou nope. they are same. Its just that when I first login i cant see the user details. But when I exit the app and relaunch it 2-3 times i see the details – user3627247 May 12 '14 at 08:27

0 Answers0