发布后StringRequest存储数据

时间:2016-09-19 14:22:26

标签: android database post response

我遇到问题,当我点击button时,第一次点击时ID始终为空,但下次点击时会显示ID。 这是代码

    public class MainActivity
    extends AppCompatActivity implements  view.OnClickListener {

    private Button buttonRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonRegister=(Button)findViewById(R.id.button);
        buttonRegister.setOnClickListener(this);


    }
    String userID="null";//i create user id
    final String URL = "https://aiman-king961.rhcloud.com/findID.php";//my url

    public String SimplePotRequest(){       StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    userID=response;//this is how i store it
                    //  Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    System.out.println(response);



                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG ).show();
                }
            }){

        @Override

        protected Map<String, String> getParams() throws AuthFailureError //i know this to pass the parameter
        {
            Map<String,String> map = new HashMap<String,String>();
            map.put("username","hasanal");//this is how i try
            return map;
        }
    };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        return userID;
    };

    @Override
    public void onClick(View v) {
        String id=SimplePotRequest();

        if(v==buttonRegister){

            Toast.makeText(MainActivity.this, id, Toast.LENGTH_LONG).show();
        }

    }
   }

关键是我想点击时将idID存储在id中,因为我想在其他功能中使用它,这是android

中登录系统的一部分

2 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您调用的函数可能是异步的。

首次按下按钮时,将在后台调用该功能。当显示吐司时,id变量仍为null

第二次按下按钮时,第一个请求将填充id变量。第二个调用已在后台运行,但id显示的是以前的数据。

要管理问题,您需要等到通话结束。在onResponse

中进行操作
@Override
public void onResponse(String response) {
    userID=response;//this is how i store it
    //  Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
    System.out.println("apekk"+response);
    buttonRegister.setText(response);

    // Make your actions here
}

答案 1 :(得分:0)

尝试使用以下代码,可以解决您的问题:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   private static final String REGISTER_URL = "http://simplifiedcoding.16mb.com/UserRegistration/volleyRegister.php";

   public static final String KEY_USERNAME = "username";
   public static final String KEY_PASSWORD = "password";
   public static final String KEY_EMAIL = "email";

   private EditText editTextUsername;
   private EditText editTextEmail;
   private EditText editTextPassword;

   private Button buttonRegister;

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

        editTextUsername = (EditText) findViewById(R.id.editTextUsername);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        editTextEmail= (EditText) findViewById(R.id.editTextEmail);

        buttonRegister = (Button) findViewById(R.id.buttonRegister);

        buttonRegister.setOnClickListener(this);
}

private void registerUser(){
    final String username = editTextUsername.getText().toString().trim();
    final String password = editTextPassword.getText().toString().trim();
    final String email = editTextEmail.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(KEY_USERNAME,username);
            params.put(KEY_PASSWORD,password);
            params.put(KEY_EMAIL, email);
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

@Override
public void onClick(View v) {
    if(v == buttonRegister){
        registerUser();
     }
  }
}

注意:sample source code