1

Trying to display MySQL data in my domain server to android app using PHP and displaying my data as a toast message. Whenever I run the app, I get empty toast. Further, I will be using a list view to display the data. There are no errors in the code. Below is the code for my AsychTask:

class Connection extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            String result = "";
            String host = "http://prasaurus.com/conn.php";

            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(host));
                HttpResponse response = client.execute(request);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer stringBuffer = new StringBuffer("");

                String line = "";

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

                reader.close();
                result = stringBuffer.toString();

            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result){
            Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
        }

    }

Here is my php code on sever end:

<?php

$db_name = "prasauru_FAND_DB";
$mysql_username = "###########"; #database name on server end
$mysql_password = "###########"; #database password
$server_name = "prasaurus.com"; 

$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);

if(!$conn){
    die("Error in connection"  . mysqli_connect_error());
}

$response = array();

$query = "SELECT * FROM `under8_club_league` ORDER BY `points` DESC";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_assoc($result)){
        array_push($response , $row);
    }
}
else {
    $response['success'] = 0;
    $response['message'] = 'No data';
}

echo json_encode($response);    
mysqli_close($conn);

?>
Prashant Luhar
  • 299
  • 5
  • 19

2 Answers2

1

Your code is not working. First you don't need use break in that while loop.

And second if you want the result in the onPostExecute method you should return it instead return null.

There is your code fixed:

 class Connection extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            String result = "";
            String host = "http://prasaurus.com/conn.php";

            try {
                URL url = new URL(host);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuffer stringBuffer = new StringBuffer("");

                String line = "";

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

                reader.close();
                result = stringBuffer.toString();

            } catch (IOException e) {
                e.printStackTrace();
            }

            return result;
        }

Note:HttpClient is not supported anymore in the new Android versions for that reason i changed this in the code.

I hope it helps you.

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
  • There is one more question in my profile, no one is answering it. It would be nice if you could just give me an idea what can be done in this ->>>> [link](https://stackoverflow.com/questions/52143795/services-for-live-streaming-videos-on-android-application-other-than-youtube) – Prashant Luhar Sep 06 '18 at 11:42
1

First of all, you have to add internet permission in the manifest file

<uses-permission android:name="android.permission.INTERNET" />

then Java code

public static JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException {
    HttpURLConnection urlConnection = null;
    URL url = new URL(urlString);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000 /* milliseconds */ );
    urlConnection.setConnectTimeout(15000 /* milliseconds */ );
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuilder sb = new StringBuilder();

    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }
    br.close();

    String jsonString = sb.toString();
    System.out.println("JSON: " + jsonString);

    return new JSONObject(jsonString);
}

And Use the function like below

try{
      JSONObject jsonObject = getJSONObjectFromURL(urlString);
 // here you can use Jsonobject or Jsonarray as per your requirement.
        } catch (IOException e) {
              e.printStackTrace();
        } catch (JSONException e) {
              e.printStackTrace();
        }
Hitesh Raviya
  • 129
  • 6
  • 13