OkHttp - Android get Data from MySQL database

Viewed 1567

Hey Im fairly new to android developing and Im currently trying to establish a connection to my database so I can work with the data on my phone.

Im using OkHttp and mysqli with php.

I wrote a handler which accesses the .php pages and makes the database calls.

public class OkHttpHandler extends AsyncTask<String, Void, String> {

private OkHttpClient client = null;

public String latestMessage = "";

public OkHttpHandler(){

    client = new OkHttpClient();
}

@Override
protected String doInBackground(String... params) {
    Request.Builder builder = new Request.Builder();
    builder.url(params[0].toString());
    Request request = builder.build();

    try{
        Response response = client.newCall(request).execute();
        latestMessage = response.body().string();
        return response.body().string();
    } catch(Exception e){
        e.printStackTrace();
    }

    //latestMessage = "failed";
    return null;
}

And I want to do something like this:

    public int getSongwriterId(String songwriter) {
    OkHttpHandler okHttpHandler = new OkHttpHandler();
    okHttpHandler.execute(url_get_songwriter_id + "?songwriter=" + songwriter);
    String responseString = "";
    while(responseString == "") {
        responseString = okHttpHandler.latestMessage;
    }
    okHttpHandler.cancel(true);
    Songwriter s = getSongwriterFromJSON(responseString);

    return s.getId();
    }

Now, I know that this ugly while(true) is a shitty solution. But how should I do it so Im 100% certain I got my responseString before passing it into "getSongwriterFromJSON"? I really don't get how that is supposed to work.

The code works all fine when i debug it step by step, but the application just blackscreens from time to time without debug. I guess thats due to the while(true) loop and that probably just runs and runs and clocks up the thread so that my background thread just dies - I really don't know anymore.

Please if you provide help, explain what Im doing wrong, and how I should do it right. Thanks.

Edit php file used:

<?php
$response = array();

$con = @mysqli_connect("XXX", "XXX", "XXX", "XXX");

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "Select * FROM songwriter ";
$sql .= "where composerName = '";
$sql .= $_GET["songwriter"];
$sql .= "'";

$query = mysqli_query($con,$sql);

// looping through all results
$response["songwriter"] = array();

while ($row = mysqli_fetch_array($query)) {
    $songwriter = array();
    $songwriter["id"] = $row["id"];
    $songwriter["composerName"] = $row["composerName"];
    $songwriter["arrangerName"] = $row["arrangerName"];

    // push single product into final response array
    array_push($response["songwriter"], $songwriter);
}

// echoing JSON response
echo json_encode($response);
?>
2 Answers
Related