不能通过$ _POST到达发送的数据(“它为空”,因此结果为空“),但已建立连接

时间:2019-01-15 14:38:42

标签: java php android android-volley

数据未发送到php文件。 条件if(isset($ _ POST ['f_name])不正确。

已成功连接到php文件,并且我对其进行了测试,但未发送数据。

MainActivity.java

$(".no2 .num1 .abc").css("color","orange);

registerUser.php

 package com.example.sarah.vent;

 import android.app.ProgressDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Spinner;
 import android.widget.Toast;

 import com.android.volley.AuthFailureError;
 import com.android.volley.Request;
 import com.android.volley.RequestQueue;
 import com.android.volley.Response;
 import com.android.volley.VolleyError;
 import com.android.volley.toolbox.StringRequest;
 import com.android.volley.toolbox.Volley;

 import org.json.JSONException;
 import org.json.JSONObject;

 import java.util.HashMap;
 import java.util.Map;

public class MainActivity extends AppCompatActivity {

String arr[]={"public","private"};
String privacy;
EditText fi_name,la_name,password,emai;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button reg=findViewById(R.id.reg);
    fi_name=findViewById(R.id.f_name);
     la_name=findViewById(R.id.l_name);
     password=findViewById(R.id.pass);
     emai=findViewById(R.id.email);
     pd=new ProgressDialog(this);
    reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onclick();
            register();
        }
    });

}


void onclick(){
    final Spinner s=(Spinner) findViewById(R.id.privacy);
    ArrayAdapter<String> m=new ArrayAdapter <String>(this,android.R.layout.simple_spinner_dropdown_item,arr);
    s.setAdapter(m);
    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            i=s.getSelectedItemPosition();
            privacy=arr[i];
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

private void register() {
    final String f_name=fi_name.getText().toString().trim();
    final String l_name=la_name.getText().toString().trim();
    final String email=emai.getText().toString().trim();
    final String pass=password.getText().toString().trim();
    Toast.makeText(this, f_name, Toast.LENGTH_SHORT).show();
    pd.setMessage("Registering user");
    pd.show();

    StringRequest str=new StringRequest(Request.Method.POST, Constants.URL_REGISTER, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            pd.dismiss();
            try {
                JSONObject jsonObject=new JSONObject(response);
                Toast.makeText(MainActivity.this, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();


            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            pd.dismiss();
            Toast.makeText(MainActivity.this, "not ok", Toast.LENGTH_SHORT).show();

            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }){
        @Override
        protected Map<String, String> getPostParams() throws AuthFailureError {
          Map <String,String> params=new HashMap<>();
           params.put("f_name",f_name);
            params.put("l_name",l_name);
            params.put("email",email);
            params.put("pass",pass);
            params.put("privacy",privacy);return params;
        }
    };

    RequestQueue rQ= Volley.newRequestQueue(this);
    rQ.add(str);
}}

数据应该到达php文件,但结果为空。尽管数据已成功通过邮递员发送。

0 个答案:

没有答案