KALYAN DEVANABOYINA
KALYAN DEVANABOYINA

Reputation: 1

HTTP post call from android to PHP?

I am new working with Android Studio. Here in the below code i am trying to send data from java to the backend server (in PHP). The call is being made but NULL is returned from the server side which shouldn't happen. So I think input sent from the java side is not interpreted properly at the PHP side i guess. Could you tell what is wrong with the code (and PHP code is perfectly all right and it should not return NULL).

          HttpURLConnection httpURLConnection = null;
          JSONObject  Student_det = stu_det.getJSONObject(i);
          String stu_GUID = Student_det.getString("StuGUID");
          String visit_GUID = Student_det.getString("VisitGUID");

            String value = "Rec="+(tableRow+1)+"&SchGUID="+schoolGUID.trim()+"&StuGUID="+stu_GUID.trim()+"&VisitGUID="+visit_GUID.trim()+"&role="+role;
            URL url = new URL("http://52.66.25.82/api/download_student.php");
            String enc_val = URLEncoder.encode(value,"UTF-8");

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            httpURLConnection.setRequestMethod("POST");



            OutputStreamWriter wr = new OutputStreamWriter(httpURLConnection.getOutputStream());
            wr.write( enc_val );
            wr.flush();

            InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());

            BufferedReader responseStreamReader =
                    new BufferedReader(new InputStreamReader(responseStream));


            // Read Server Response
         String output = responseStreamReader.readLine();

Upvotes: 0

Views: 1613

Answers (3)

Narendra Yadav
Narendra Yadav

Reputation: 52

HTTP post call from android to PHP. Start with best possible way.

http://www.androidcode.in/php-full-code-with-login-and-registration-in-android

public class LoginAsy extends AsyncTask <String, Void, String > {
        Context myContext;
        String result;

        public LoginAsy(Context c) {
            myContext = c;
        }

        @Override
        protected String doInBackground(String... params) {

            String myurldata = "name=" + params[0] + "&email=" + params[1] + "&mobile=" + params[2] + "&username=" + params[3] + "&password=" + params[4];

            try {
                URL url = new URL("http://192.168.2.3/Android/jnuproject.php");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.getOutputStream().write(myurldata.getBytes());

                int responce = connection.getResponseCode();

                Log.d("Responce code ", "" + responce);

                if (responce == HttpURLConnection.HTTP_OK) {
                    String line;
                    StringBuilder builder = new StringBuilder();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                    while ((line = reader.readLine()) != null) {
                    builder.append(line);
}
                    result = builder.toString();
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (result.equals("success")) {
                login = true;
            } else {
                login = false;
            }
            return null;
        }
    }

Upvotes: 0

Sagar V.S.
Sagar V.S.

Reputation: 246

To make REST full Api's call from Android App use Retrofit by Square.inc which is the fastest and best way to call web services in android app

Check out this Easy Retrofit Sample app to call Api's -

https://github.com/TheSeasApps/Easy-Retrofit

Upvotes: 0

Girish
Girish

Reputation: 100

As you said new to android, start with best possible way

Try to implement with volley library or RetroFit lib,its easy and simple

volley example

OR

RetroFit example

Upvotes: 1

Related Questions