简体   繁体   中英

How to connect an android application with a local server using volley api

I am working on an application that requires logging in and signing up, using the volley library.

Testing the application I entered my credentials and signed up but it did not post my details to my local server and I am getting a toast message in the application "com.android.volley.servererror"

Here is my volleyRegister.php file :

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
require_once('dbConnect.php');
$sql = "INSERT INTO android-volley (fullname,email,username,password) VALUES ('$fullname','$email','$username','$password')";
if(mysqli_query($con,$sql)){
    echo "Successfully Registered";
}else{
echo "Could not register";
}}else{
echo 'error';}?>

Android code

public class SignUpFragment extends Fragment implements Button.OnClickListener {


public static final String Register_URL ="http://192.168.0.107/android-connect/volleyRegister.php";



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



EditText etFullName;
EditText etEmail;
EditText etUsername;
EditText etPassword;
Button SignUp2;


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_signup, container, false);

    final Animation animAlpha = AnimationUtils.loadAnimation(getActivity(), R.anim.anim_alpha);

    etFullName = (EditText) rootView.findViewById(R.id.etFullName);
    etEmail = (EditText) rootView.findViewById(R.id.etEmail);
    etUsername = (EditText) rootView.findViewById(R.id.etUsername);
    etPassword = (EditText) rootView.findViewById(R.id.etPassword);
    Button SignUp2 = (Button) rootView.findViewById(R.id.Signup2);


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

            final String fullName = etFullName.getText().toString();
            final String email = etEmail.getText().toString();
            final String username = etUsername.getText().toString();
            final String password = etPassword.getText().toString().trim();

            AnimationSet sets = new AnimationSet(false);
            sets.addAnimation(animAlpha);
            v.startAnimation(sets);

            if (etFullName.length() == 0 || etEmail.length() == 0 || etUsername.length() == 0 || etPassword.length() == 0) {
                Toast.makeText(getActivity(), "Oops! fill in complete details", Toast.LENGTH_SHORT).show();


            } else {
                StringRequest stringRequest = new StringRequest(Request.Method.POST, Register_URL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String s) {
                                Toast.makeText(SignUpFragment.this.getActivity(), s, Toast.LENGTH_LONG).show();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Toast.makeText(SignUpFragment.this.getActivity(), volleyError.toString(), Toast.LENGTH_LONG).show();
                            }


                        }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<>();
                        params.put(KEY_FULLNAME, fullName);
                        params.put(KEY_EMAIL, email);
                        params.put(KEY_USERNAME, username);
                        params.put(KEY_PASSWORD, password);
                        return params;
                    }


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

    return rootView;
}

`

open cmd and type ipconfig in your php system(pc or lappy)

copy your ip address

and use it like

//test ip 192.168.0.1

private void callvolly() {
    String url = "http://192.168.0.1/thenyourfolderpath";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {


                    //responce

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("", "");//params
            Log.e("send", params.toString());
            return params;
        }

    };
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            20000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

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