从android到PHP的HTTP post调用?

时间:2017-10-24 08:58:53

标签: java php android http post

我是Android Studio的新成员。 在下面的代码中,我试图将数据从java发送到后端服务器(在PHP中)。正在进行调用,但是从服务器端返回NULL,这不应该发生。所以我认为从Java方面发送的输入在PHP方面没有被正确解释。你能告诉我代码有什么问题吗(PHP代码完全正常,它不应该返回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();

3 个答案:

答案 0 :(得分:1)

正如你所说的Android新手,以最好的方式开始

尝试使用volley库或 RetroFit lib,简单易用

volley example

OR

RetroFit example

答案 1 :(得分:0)

要从Android应用程序进行REST完整Api的调用,请使用Square.inc的Retrofit,这是在Android应用程序中调用Web服务的最快和最好的方法

查看此Easy Retrofit Sample应用程序以致电Api' s -

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

答案 2 :(得分:0)

从android到PHP的HTTP post调用。从最好的方式开始。

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;
        }
    }