在第一次点击时发送凌空不发送参数的请求

时间:2015-06-16 16:58:20

标签: android http-post android-volley

我正在android studio中制作一个应用程序,我正在使用排球库来点击按钮发布帖子请求。

第一次按下按钮时,响应始终为空,但第二次正常工作。

以下是代码:

public class LoginActivity extends Activity{
Button btnEnviar;
EditText domn;
EditText user;
EditText pass;
String respuesta=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);


    btnEnviar = (Button)findViewById(R.id.btnLogin);
    domn = (EditText)findViewById(R.id.txtDominio);
    user = (EditText)findViewById(R.id.txtUser);
    pass = (EditText)findViewById(R.id.txtPassword);
    btnEnviar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String dominio = domn.getText().toString();
            String auth=loginRequest(("server url"));

            Log.i("Server response:", "" + auth);


            /*Intent i = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);*/
        }
    });
}

public String loginRequest(String url){
    ;
    StringRequest request = new StringRequest(Request.Method.POST,url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    respuesta = s;
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError ex) {
                   // System.out.println(ex.getMessage().toString());
                }
            }
    ){
        @Override
        protected Map<String,String> getParams(){
            String usr = user.getText().toString();
            String psw = pass.getText().toString();
            Map<String,String> params = new HashMap<String, String>();
            params.put("User",usr);
            params.put("Pass",psw);
            params.put("DeviceName","Nombre");
            params.put("DeviceType","Tipo");
            params.put("OSVersion","Version");
            params.put("VendorID","ID");
            Log.i("USER:",usr);
            Log.i("PASS:", psw);

            return params;
    }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            return params;
        }
    };

    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);
    return respuesta;
}

我也得到这个日志回复:

首次点击:

06-16 10:09:33.325  14628-14628/? I/Server response:﹕ null
06-16 10:09:33.375  14628-14744/? I/USER:﹕ username
06-16 10:09:33.375  14628-14744/? I/PASS:﹕ password

第二次点击:

06-16 10:09:48.945  14628-14628/? I/Server response:﹕ 1
TOKEN
06-16 10:09:48.955  14628-14759/? I/USER:﹕ username
06-16 10:09:48.955  14628-14759/? I/PASS:﹕ password

有关如何修复它的任何想法?感谢。

1 个答案:

答案 0 :(得分:0)

您的返回字符串I/Server response:﹕ 1是异步设置的。

您所看到的onResponse实际上是您从第一次按下按钮时得到的回复。

您应该将日志移至public void onResponse(String s) { respuesta = s; //todo add log and logic here }

var start = [
    {
        type:   "top_img",
        child:  [
            {STRING
                id:"001",
                title:"POD Deck Lite-B1 Service Manual Rev0.0",
                href:"../frame_images/A021POD2-COVER-0001.gif"
            }
        ]
    },{
        type:   "menu",
        child:  [
            {
                id:"001",
                title:"Technology",
                description:"Technology"
            },{
                id:"002",
                title:"Service",
                description:"Service"
            },{
                id:"003",
                title:"Appendix",
                description:"Appendix"
            }
        ]
    },{
        type:   "reference",
        child:  [
            {
                id:"001",
                title:"How to use",
                src:"how to use",
                href:"../frame_htmls/how to use.html"
            },{
                id:"002",
                title:"Read me",
                src:"readme",
                href:"../frame_htmls/readme.html"
            },{
                id:"003",
                title:"Site map",
                src:"sitemap",
                href:"../frame_htmls/sitemap.html"
            },{
                id:"004",
                title:"History",
                src:"history",
                href:"../frame_htmls/history.xls"
            }
        ]
    },{
        type:   "header",
        child:  [
            {
                id:"001",
                title:"Home",
                href:"../esm.htm"
            },{
                id:"002",
                title:"Site map",
                href:"../frame_htmls/sitemap.html"
            }
        ]
    }

];