阅读Android的POST请求的回声

时间:2017-05-20 16:16:37

标签: java android post android-volley

我真的需要你昨天阻止的帮助:'(

// MY X VAR

int x ;

//我将check_referrer()函数执行到我的按钮点击列表器

   signupJoinBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            username = signupUsername.getText().toString();
            referrercode = signupReferrer.getText().toString();

            x =check_referrer(username,referrercode);

// Check_Reinrer应该从我的PHP文件读取“1”或“0”(我测试了我的php文件和他的工作)

public int check_referrer( String referral, String referrer  ) {


String awr = Config.Base_Url + "get/checkref.php";

 final String v1 = "1"; // OK
 final String v0 = "0"; // The referrer is not existing

    StringRequest stringRequest = new StringRequest(Request.Method.POST,awr,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.equals(v1) ) {

                            x= 1;
                    }
                    else  if (response.equals(v0) ) {

                        x= 0;
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }) {
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

               // user is the variable my php file will receive
            params.put("user",username);
                // user is the variable my php file will receive
            params.put("ref", referrercode);
            return params;
        }
    };

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

    return x;
}

1 个答案:

答案 0 :(得分:0)

您必须声明

JSONObject jObj = new JSONObject(response);
你的onResponse方法中的

。然后你可以使用

从php文件中提取Json值
String var1 = jObj.getString("var1");

你能告诉我们你的php文件吗?