简体   繁体   中英

Error while posting variable from android client to php server

I want to make an activity to change user profil. The received part which is based on JSON, is properly working. I have tested by set variable manually in PHP code. However, when I have posted the variable from android to php, it cannot receive it. Anyone can tell me the problem ?

public class ProfilActivity extends AppCompatActivity {

private EditText editTextNama, editTextEmail, editTextPassword, editTextNohp;
private Button Simpan;
private static final String PROFIL_URL = "http://vrai-dev.000webhostapp.com/koneksi_profil.php";

List<Profil> profilList;

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

        Button back = (Button) findViewById(R.id.profil_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(ProfilActivity.this, MenuActivity.class);
                startActivity(i);
            }
        });

        profilList = new ArrayList<>();

        getEmail();

        editTextNama = (EditText) findViewById(R.id.profil_username);
        editTextEmail = (EditText) findViewById(R.id.profil_email);
        editTextPassword = (EditText) findViewById(R.id.profil_password);
        editTextNohp = (EditText) findViewById(R.id.profil_nohp);

        Simpan = (Button) findViewById(R.id.profil_simpan);
        Simpan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    updateProfil();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void getEmail(){
        final String email = "a";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, PROFIL_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        loadProfil();
                    }
                },
                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("email", email);
            return params;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void loadProfil() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, PROFIL_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray profil = new JSONArray(response);

                    JSONObject profilObject = profil.getJSONObject(0);

                    String foto_user = profilObject.getString("foto_user");
                    String username = profilObject.getString("username");
                    String email = profilObject.getString("email");
                    String password = profilObject.getString("password");
                    String nohp = profilObject.getString("nohp");

                    Profil viewProfil = new Profil(foto_user, username, email, password, nohp);
                    profilList.add(viewProfil);

                    editTextNama.setText(username);
                    editTextEmail.setText(email);
                    editTextPassword.setText(password);
                    editTextNohp.setText(nohp);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ProfilActivity.this, error.getMessage() + "Error Load Profil", Toast.LENGTH_LONG).show();
            }
        });
        Volley.newRequestQueue(this).add(stringRequest);
    }

Had the same problem could not figure what was wrong, but this worked for, dont forget to add the dependency in the gradle file

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

    StringRequest postRequest = new StringRequest(com.android.volley.Request.Method.POST, YOUR_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                     // do work here
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d("Response", "failed: " + response);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            //add your parameters here as key-value pairs
            params.put("title", title);
            return params;
        }
    };
    queue.add(postRequest);

** implementation 'com.he5ed.lib:volley:android-cts-5.1_r4' **

Try to modify your loadProifle() method like below

     // define below variable to globally
     String mRequestBody=null;


     private void loadProfil() {
    try {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("namekey", "paste here name");
        jsonBody.put("emailkey", "paste here email");
        jsonBody.put("passkey", "paste here password");
        jsonBody.put("nokey", "paste here number");
        mRequestBody = jsonBody.toString();
    }catch(JSONException e){
        e.printStackTrace();
    }
    StringRequest stringRequest = new StringRequest(Request.Method.POST, PROFIL_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray profil = new JSONArray(response);

                JSONObject profilObject = profil.getJSONObject(0);

                String foto_user = profilObject.getString("foto_user");
                String username = profilObject.getString("username");
                String email = profilObject.getString("email");
                String password = profilObject.getString("password");
                String nohp = profilObject.getString("nohp");

                Profil viewProfil = new Profil(foto_user, username, email, password, nohp);
                profilList.add(viewProfil);

                editTextNama.setText(username);
                editTextEmail.setText(email);
                editTextPassword.setText(password);
                editTextNohp.setText(nohp);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(ProfilActivity.this, error.getMessage() + "Error Load Profil", Toast.LENGTH_LONG).show();
        }
    }){
        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        }
    };
    Volley.newRequestQueue(this).add(stringRequest);
}

Here is my php code for getting email variable. Is there something wrong?

<?php

define('DB_HOST','Localhost');
define('DB_USER','id9815170_phpjember');
define('DB_PASS','phpjember');
define('DB_NAME','id9815170_phpjember');

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if(mysqli_connect_errno()){
    die('Unable to connect to database ' . mysqli_connect_error());
}

$email = $_POST['email'];

$stmt = $conn->prepare("SELECT foto_user, username, email, password, nohp FROM datauser WHERE email='$email';");

$stmt->execute();

$stmt->bind_result($foto_user, $username, $email, $password, $nohp);

$profil = array();

while($stmt->fetch()){

    $temp = array();
    $temp['foto_user'] = $foto_user;
    $temp['username'] = $username;
    $temp['email'] = $email;
    $temp['password'] = $password;
    $temp['nohp'] = $nohp;

    array_push($profil, $temp);
}
echo json_encode($profil);

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM