无法从后端获得有关我的查询的响应。我该如何解决这个问题?

时间:2017-08-19 13:04:36

标签: java android mysql android-volley

我正在尝试创建一个使用volley和PHP连接到mySQL的注册活动。我附上了一些代码。代码完全正常,直到我尝试从数据库中获取响应。使用StringRequest时,控件不会进入代码的这一部分。我该如何解决这个问题?请帮助。

#define DATA  0
#define STRING  1
#define MAT  2
#define ENTRY  3
#define EXTERN  4

将请求添加到队列中的MySingleton类文件。

public class RegisterActivity extends AppCompatActivity {

String rurl = "http://102.160.2.104/register.php";
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    final EditText etName = (EditText) findViewById(R.id.etName);
    ....
    final EditText weig = (EditText) findViewById(R.id.weight);
    final Button bRegister = (Button) findViewById(R.id.bRegister);

    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String name = etName.getText().toString();
            final String username = etUsername.getText().toString();
            ...

            if(!(conpassword.equals(password))){
                etPassword.setText("");
                etconpas.setText("");
                Toast.makeText(getApplicationContext(),"Passwords don't match",Toast.LENGTH_LONG).show();
            }

//The problem arises over here as it does not enter the onResponse() method.

            StringRequest stringRequest = new StringRequest(Request.Method.POST, rurl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        message = jsonObject.getString("message");
                        fin();
                    } catch (JSONException e) {
                        System.out.println("hhellooo");
                        e.printStackTrace();
                    }
                }

                private void fin() {
                    System.out.println(message);
                    if(message.equals("User already exists")){
                        etName.setText("");
                        etPassword.setText("");
                        contact.setText("");
                        etUsername.setText("");
                        age.setText("");
                        heig.setText("");
                        weig.setText("");
                        Toast.makeText(getApplicationContext(),"User already Exists",Toast.LENGTH_LONG).show();
                    }

                    else
                    {
                        Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<>();
                    params.put("name",name);
                    params.put("username",username);
                    params.put("password",password);
                    params.put("value", finalValue);
                    params.put("phone", String.valueOf(phone));
                    params.put("years", String.valueOf(years));
                    params.put("height", String.valueOf(height));
                    params.put("weight", String.valueOf(weight));
                    return params;
                }
            };
            MySingleton.getInstance(RegisterActivity.this).addtoRQ(stringRequest);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我认为您的请求未被发送,因为您尚未将其添加到请求队列中。

RequestQueue mRequestQueue;

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());

// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);

// Start the queue
mRequestQueue.start();

<Your request setup>

// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);

来自:Android Developer