简体   繁体   中英

Not getting value after whitespace while passing variable from android to php

I am trying to pass a variable from android to php, while receiving value in php does not get values after whitespace. I am passing variable like,

private void register(String txtname) {
String urlSuffix = "?name="+txtname;
 Log.d("unmae",txtname);
    class RegisterUser extends AsyncTask<String, Void, String>{
        ProgressDialog loading;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(UserRegister.this,"Please Wait","Loading...");
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
           loading.dismiss();
                      }

        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferReader = null;
            try {
                URL url = new URL(REGISTER_URL+s);
                HttpURLConnection con= (HttpURLConnection) url.openConnection();
                bufferReader=new BufferedReader(new InputStreamReader(con.getInputStream()));
                String result;
                result = bufferReader.readLine();
                return result;

            } catch (Exception e) {

                return null;
            }
        }
    }

        RegisterUser ru = new RegisterUser();
        ru.execute(urlSuffix);
}

On Php side

$uname = $_GET['name'];

While log the variable, the output is like this Eg: unmae:first second

whie the echo in php side it appears like this

Eg: $uname = $_GET['name']; echo $uname; output:first

Try to encode the String variable that you are ussing for name. This will prevent bad characters or replace spaces by "+" or "%20"

ru.execute(URLEncoder.encode(urlSuffix, "utf-8"));

Or better, use an URI builder

// This will get the final URL for request
String uri = Uri.parse(REGISTER_URL)
                .buildUpon()
                .appendQueryParameter("name", txtname)
                .build().toString();
  1. You should use the URLEncoder

     String urlSuffix= URLEncoder.encode(name, "UTF-8"); 
  2. Specify the method as GET

     String URL = "http://android.com/android_test/httpget.php? +name=urlSuffix" URL url=new URL(ur_url); urlConnection= (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); 

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