0

I am sending user email from Android app to PHP using HttpUrlConnection but PHP is not receiving Any data from App. This type of questions have already been asked but their solution do not worked for me. My Android Coding is

URL server_url = new URL("http://www.myURL.com/Jobs/login.php");

              HttpURLConnection urlc = (HttpURLConnection) server_url.openConnection();
                urlc.setDoOutput(true);
                urlc.setDoInput(true);
                urlc.setRequestMethod("POST");
                urlc.setRequestProperty("Content-Language", "en-US");
                urlc.setRequestProperty("Accept-Encoding", "identity");
                urlc.connect();

                HashMap<String, String> param = new HashMap<>();
                param.put("email", mEmail);

               DataOutputStream os = new DataOutputStream(urlc.getOutputStream());

                os.writeBytes(URLEncoder.encode(mEmail, "UTF-8"));


                os.flush();
                os.close();

and My php code is:

<?php
        $user_email=$_POST['email'];
        echo "Email is $user_email";
?>

but when running this php on browser, it is echoing "Email is" as it is not receiving any data from android. Please Help My php code contains only these two lines. Am I missing something in php coding?

  • `mEmail` isn't defined anywhere? – IsThisJavascript Sep 05 '17 at 10:11
  • mEmail is variable in which user email is saved – meenakshi agrawal Sep 05 '17 at 10:36
  • You're not using your `param` variable at all after you've set it – crazyloonybin Sep 05 '17 at 10:38
  • @crazyloonybin Yes, that was my mistake.and I have corrected it but still the problem is not solved – meenakshi agrawal Sep 05 '17 at 15:50
  • @meenakshiagrawal I'd recommend looking at the answers on [this question](https://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post). The first answer uses code that is deprecated from API v22, but the answer below that has updated code for later API versions. – crazyloonybin Sep 05 '17 at 15:52
  • @crazyloonybin not getting you. Please explain.i am stucked in this problem..please help – meenakshi agrawal Sep 05 '17 at 15:56
  • @meenakshiagrawal the question I linked to in the previous comment has answers detailing how to post data from Android - try those answers out and see if that works for you. – crazyloonybin Sep 05 '17 at 15:57
  • @crazyloonybin that linked is based on parameters..I found no solution – meenakshi agrawal Sep 05 '17 at 16:10
  • @meenakshiagrawal have you actually tried the solutions on the page to see if they work? You say the link is based on parameters, but that's what you need - you're trying to send the `email`/`mEmail` parameter. There is also [this tutorial](https://www.studytutorial.in/android-httpurlconnection-post-and-get-request-tutorial) that uses similar code to the link above for sending a POST request. – crazyloonybin Sep 06 '17 at 08:05
  • @crazyloonybin I tried the tutorial but still my problem is not solved – meenakshi agrawal Sep 07 '17 at 10:03

2 Answers2

0

You're not sending the paramater at all. You need to structure the request much better aswell since it's clear you're getting confused.

    URL server_url = new URL("http://www.myURL.com/Jobs/login.php");

          HttpURLConnection urlc = (HttpURLConnection) server_url.openConnection();
            //header stuff
            urlc.setRequestMethod("POST");
            urlc.setRequestProperty("Content-Language", "en-US");
            urlc.setRequestProperty("Accept-Encoding", "identity");
            //params
            String urlParameters = "email="+mEmail;
            //send post
            urlc.setDoOutput(true);
             DataOutputStream wr = new DataOutputStream(urlc.getOutputStream());
             wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();   

            //read result
            BufferedReader in = new BufferedReader(
                new InputStreamReader(urlc.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                     response.append(inputLine);
                }
                in.close();

                //print result
                System.out.println(response.toString()
           );
IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25
0

After so many solutions and discussion finally i got the solution..can not say solution but an alternative approach and it is " volley" library...i used it..and finally php receives the data....