0

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.

6
  • yes am checking.. i'll reply you. thank you. Commented Nov 17, 2015 at 5:28
  • Are you getting any problem or it is workable for you?? Commented Nov 17, 2015 at 5:47
  • ya its working.. thank you so much. now i want to diaply each of the values in different text boxes or store those values into different variables. how to do that. can you suggest something.? Commented Nov 17, 2015 at 6:19
  • For storing the value you can use the the sqlite or sharedpreference...Note these thing only use when you want to store the data for future perspective like for saving the login and password. Commented Nov 17, 2015 at 6:25
  • ok then what do i do now..? i want to display these values into their textboxes in another activity. Commented Nov 17, 2015 at 6:28

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

There might be more than one issue but a big one is you never send the request, all you are doing is creating the request and setting up the parameters

you need to do a

conn.connect();

or

conn.getInputStream();

or any number of things you can do to send the request and get the information you require

AFTER you write your params

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.