使用齐射,身份验证始终失败

时间:2014-09-16 01:54:50

标签: android json string http android-volley

我是编程新手。我正在编写一个Android应用程序,它将使用我的帐户的用户名和密码登录到私人服务器(网页)。如果用户名和密码是正确的'我可以使用手机在该网页上查看我的帐户。我正在使用Volley,以下是我登录的代码:

public class MainActivity extends Activity {

private Button mTrigger;
private RequestQueue mVolleyQueue;
private ProgressDialog mProgress;
private final String TAG_REQUEST = "MY_TAG";

private TextView mResultView;
private EditText uname;
private EditText pword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBarSetup();


    mVolleyQueue = Volley.newRequestQueue(this,new SslHttpStack(true));

    mResultView = (TextView) findViewById(R.id.result_txt);

    uname = (EditText)findViewById(R.id.txt_username);
    pword = (EditText)findViewById(R.id.txt_password);

    mTrigger = (Button) findViewById(R.id.button1);
    mTrigger.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showProgress();
            makeSampleHttpRequest();
        }
    });
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void actionBarSetup() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    ActionBar ab = getActionBar();
    ab.setTitle("AIM User Login");
  }
}

public void onDestroy() {
    super.onDestroy();
}

public void onStop() {
    super.onStop();
    if(mProgress != null)
        mProgress.dismiss();

    mVolleyQueue.cancelAll(TAG_REQUEST);
}

private void showProgress() {
    mProgress = ProgressDialog.show(this, "", "Loading...");
}

private void stopProgress() {
    mProgress.cancel();
}



private void makeSampleHttpRequest() {

    final String param1 = uname.getText().toString();
    final String param2 = pword.getText().toString();
    final String url = "http://vo.aimglobalinc.com/control/con_login.asp";



    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            mResultView.setText(response);
            stopProgress();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            if( error instanceof AuthFailureError) {
                stopProgress();
                //showToast(error.getMessage());
                Toast.makeText(MainActivity.this, "Please check your username or password.", Toast.LENGTH_LONG).show();

            } else if( error instanceof ServerError) {
            } else if( error instanceof NetworkError) {
            } else if( error instanceof ParseError) {
            } else if( error instanceof NoConnectionError) {
            } else if( error instanceof TimeoutError) {
            }

            stopProgress();
            //showToast(error.getMessage());
            Toast.makeText(MainActivity.this, "Please check your internet connection.", Toast.LENGTH_LONG).show();
        }   
    }) {

        protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json; charset=utf-8");
            params.put("username", param1);
            params.put("password", param2);
            return params;
        }
    };



    stringRequest.setShouldCache(true);
    stringRequest.setTag(TAG_REQUEST);  
    mVolleyQueue.add(stringRequest);
}

}

这是我从谷歌浏览器(我正在使用的浏览器)开发者工具选项卡获得的网页的index.js:

  function login(){
        if($('#username').val()=='' || $('#password').val()==''){
            alert('Invalid username or password');
            return;
        }

        $.post('control/con_login.asp', 
            {uname: $('#username').val(), pword: $('#password').val() },
            function(jdata){            

                    if(jdata.success=='true') {
                        window.location.replace("r2bs.asp");
                    } else {
                        alert(jdata.message);
                    }
            }, 'json');

}

我的问题是,即使我输入了我的帐户的正确用户名和密码,或者如果我输错了用户名或密码,我得到的响应(使用模拟器)总是如下:

  {"success":"failed","message":"Invalid username or password"}

我至少应该看到&#34;成功&#34;或该网页上我帐户中的字符串。请帮助我或纠正我的代码和方法,如何做我的项目。提前致谢。

0 个答案:

没有答案