簡體   English   中英

如何在我的 android 片段中使用 echo php

[英]How to use echo php to my android fragment

我想回聲MySQL我連接到Android片段RecyclerView到另一個片段的布局..但是,當我開始嘗試AsyncRetrieve的Android開辟了新的類AsyncRetrieve和實施execute中的它,我試圖使變化如果AsyncRetrieve課程是一項活動,但我失敗了,所以如果有人有這方面的經驗,請推薦一個解決方案

我將inflater更改為View v =

     public View onCreateView(LayoutInflater inflater, ViewGroup 
   container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_redirect, container, 
    false);

           new AsyncRetrieve().execute();
    return v;

然后我試圖編碼這個


private class AsyncRetrieve extends AsyncTask<String, String, String> {
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn;
    URL url = null;

    //this method will interact with UI, here display loading message
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }

    // This method does not interact with UI, You need to pass result to onPostExecute to display
    @Override
    protected String doInBackground(String... params) {
        try {
            // Enter URL address where your php file resides
            url = new URL("http://192.168.1.7/example.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
        }
        try {

            // Setup HttpURLConnection class to send and receive data from php
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            // setDoOutput to true as we recieve data from json file
            conn.setDoOutput(true);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

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

                // Pass data to onPostExecute method
                return (result.toString());

            } else {

                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }


    }

    // this method will interact with UI, display result sent from doInBackground method
    @Override
    protected void onPostExecute(String result) {

        pdLoading.dismiss();
        if(result.equals("Success! This message is from PHP")) {
            textPHP.setText(result.toString());
        }else{
            // you to understand error returned from doInBackground method
            Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
        }

    }

}

}

你從 PHP 得到任何回應嗎? 如果您收到,則使用以下方法在日志中打印該響應。

    protected void onPostExecute(String result) {
}

然后你綁定到 RecyclerView。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM