代码之家  ›  专栏  ›  技术社区  ›  no_profile

如何将POST请求从Android发送到PHP

  •  0
  • no_profile  · 技术社区  · 5 年前

    我似乎无法在网上找到最近的教程,因为它们中的大多数(例如HttpClient)都被贬低了。

    这是我的联系方式:登录学生.php

    <?php
    
    include('./db/dbconn.php');
    
    $stud_idno = $_POST["idno"];
    $stud_passw = $_POST["passw"];
    
    $row=getStudent($stud_idno, $stud_passw);
    header('Content-Type: application/json');
    echo json_encode(array('student'=>$row));
    
    ?>
    

    从我的数据库中获取学生信息的代码:数据库连接.php

    function getStudent($idno, $passw){
        global $conn;
        $row;
        try{
            connect();
            $sql="SELECT * FROM student_tbl WHERE stud_idno=? and stud_passw=?";
            $stmt=$conn->prepare($sql);
      $stmt->execute(array($idno, $passw));
            $row=$stmt->fetch(PDO::FETCH_ASSOC);
        }catch(PDOException $e){ echo $e->getMessage();}
        return $row;
    }
    

    我的android应用程序所做的是显示一个alertdialog,请求localhost(已经修复),然后显示另一个对话框,请求他们登录。

    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    
        ListView listView;
        ArrayList<QuestionItem> arrayList = new ArrayList<QuestionItem>();
        ArrayAdapter<QuestionItem> adapter;
        String ip;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //allow a parallel thread to run alongside
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
    
            //
            listView = findViewById(R.id.listview_questions);
            adapter = new ArrayAdapter<QuestionItem>(this, android.R.layout.simple_list_item_1, arrayList);
            listView.setAdapter(adapter);
    
            listView.setOnItemClickListener(this);
    
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
    
    //        localhostDialog(); //
            loginDialog();
    
        }
    
        public void localhostDialog(){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.dialog_localhost, null);
            builder.setView(dialogView);
    
            final EditText localhost = dialogView.findViewById(R.id.edit_localhost);
            builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MyUtility.ipaddress = localhost.getText().toString();
    
                    try {
                        URL url = new URL("http://"+MyUtility.ipaddress+"/skillstest42/db/getall.php");
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    
                        InputStream is = conn.getInputStream();
                        StringBuffer data = new StringBuffer();
                        int ch=0;
                        while ((ch=is.read())!=-1){
                            data.append((char)ch);
                        }
    
                        is.close();
                        conn.disconnect();
    Toast.LENGTH_SHORT).show();
    
                        //..........
    
                            }
    
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            builder.show();
        }
    
        public void loginDialog(){
            AlertDialog.Builder sbuilder = new AlertDialog.Builder(this);
            LayoutInflater sinflater = this.getLayoutInflater();
            final View studentView = sinflater.inflate(R.layout.dialog_student, null);
            sbuilder.setView(studentView);
    
            final EditText stud_idno = studentView.findViewById(R.id.edit_idno);
            final EditText stud_passw = studentView.findViewById(R.id.edit_passw);
    
           //.....
    
            sbuilder.show();
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
           //....
    
    }
    

    我怎么可能从我的android应用程序发送POST请求?感谢您在此场景中提供的任何帮助。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Mehrdad    5 年前

    您可以使用AsyncHttpClient库

    https://loopj.com/android-async-http/

    String url = "http://www.yourdomain/loginstudent.php";
    RequestParams params = new RequestParams();
        params.put("username", YOURTEXT);
        params.put("password", lng);
        AsyncHttpClient client=new AsyncHttpClient();
        client.post(url, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d(TAG, "onFailure:CrachReporter " + throwable.toString());
            }
    
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
    
    
            }
        });