sending values ​​from Android to php without value of nominal value

advertisements

I want to send data from android to php.Am using android api 23 and from android's developers site a got to know that namepairvalue is deprecated from api 22 and above. I referred to one of the post here in stackoverflow and got the below code. but still it doesn't work.

My problem is that am sending a value(ID) to php code and based on that value fetching the records from database. Now if i fetch data without using this value it works fine, but i am unable to send the id value to php code.

i am unable to find whats wrong in this code or may be my fault is editing the code. It would be great if someone helps me to understand it clearly .

Thank you in advance.

Here is the code snippet that i have tried.

Android Code:

String sid = staffid.toString(); // this sid is passed as intent from another activity and passing it to below link.
Log.d("SID :", "" + sid);
 try {
            URL url = null;
            url = new URL("http://usplhubli.96.lt/hrm/hrm_app/individual_view_staff.php");
            Log.d("url to display :", "" + url.toString());
            HttpURLConnection conn = null;
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(CONNECTION_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            // Log.d("os to display :", "" + os.toString());

            Uri.Builder builder = new Uri.Builder().appendQueryParameter("sid", sid);
            Log.d("builder to display :", "" + builder.toString());

            String query = builder.build().getEncodedQuery();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            Log.d("writer display :", "" + writer.toString());
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();

        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (ProtocolException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
        return null;
    }

I have this code in doInBackground(String... params) of AsyncTask based on this sid value i have to get the data from database and display it in android activity.

Here is the PHP code that i have tried

PHP Code:

$response=array();
$sid=$_POST["sid"];   // this is the POST data that am send from android
//$sid="4";     // this is the static value that i have tried and its working
print_r($sid);
include 'dbconfig.php';

$imagePath='http://www.usplhubli.96.lt/hrm';
$query=mysqli_query($conn,"select * from hrm_staff where sid='$sid'");
if(mysqli_num_rows($query) >0) {
 $response["hrm_staff"] = array();
 while ($row = mysqli_fetch_array($query)) {
 $recp = array();
 $recp["sid"]=$row["sid"];
 $fullName=$row['fname']."".$row['mname']."".$row['lname'];
 $recp["name"]= $fullName;
 $recp["address"]=$row['address'];
 $recp['city']=$row['city'];
 $recp["design"]=$row['did'];
 $recp["contact"]=$row['mobile'];
 $recp["email"]=$row['email'];
 $recp['qualification']=$row['quali'];
 $recp["dateofjoining"]=$row['doj'];
 $ppic1=$row['ppic'];
 $ppic1;
 $ppic21=$imagePath."/".trim($ppic1);
 $recp["ppic"]= $ppic21;
 array_push($response["hrm_staff"], $recp);

 }
  $response["success"] = 1;
 $response["message"]="display records";
 echo json_encode($response);
 }
else
 {
    $response["success"] = 0;
    $response["message"] = "No products found";
    echo json_encode($response);
}

Please suggest. Thank you.


use these lines of code for using HttpURLConnection

For sending the request parameter are as:-

  Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("phone", number)
                    .appendQueryParameter("password", password)
                    .appendQueryParameter("device_login",value);

And for the getting the request parameter in the post method of connectionUrl are as follow:-

URL url = new URL(myurl);
        HttpURLConnection urlConnection = (HttpURLConnection)     url.openConnection();
        urlConnection.setReadTimeout(30000);
        urlConnection.setConnectTimeout(30000); ;
        String query = builder.build().getEncodedQuery();

                System.out.println("REQUEST PARAMETERS ARE===" + query);
                urlConnection.setRequestMethod("POST");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);

                urlConnection.connect();

                //Send request
                DataOutputStream wr = new DataOutputStream (
                        urlConnection.getOutputStream ());
                wr.writeBytes (query);
                wr.flush ();
                wr.close ();

                //Get Response
                InputStream isNew = urlConnection.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(isNew));

                String line;
                response = new StringBuffer();
                while((line = rd.readLine()) != null) {
                    response.append(line);

                }
                rd.close();

                System.out.println("Final response===="+response);