简体   繁体   中英

How to use echo php to my android fragment

I am trying to echo MySQL to my android fragment connected to RecyclerView to another Fragment for the layout.. but when I start trying AsyncRetrieve android opened a new class for AsyncRetrieve and implemented the execute inside of it I tried to make the changes as if AsyncRetrieve class is an activity but I failed so if anyone has experience in this please recommend a solution

I changed inflater to 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;

then I'm trying to code this


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

    }

}

}

R u getting any response from PHP? If you are getting then print that response in log by using below method.

    protected void onPostExecute(String result) {
}

then you bind to RecyclerView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM