John
John

Reputation: 1

Database to android, with php

i'm trying to send values from a database to an Android app. I started from a tutorial and got here, but i'm getting: "error parsing data org.json.jsonexception end of input at character 0 of" I probably have other errors, but this is the first one i get when running the app.

edit: i can't delete this question, but it is no longer relevant. The code had mistakes which i corrected meanwhile. The answers, while helpful, were not the solution. this is probably my fault, as the question was formulated badly.

Upvotes: 0

Views: 180

Answers (3)

Rarw
Rarw

Reputation: 7663

The error is occurring because there is no JSON data being received to parse. Likely this is because you are trying to connect to the internet from within your onCreate() method. Since API 11, all I/O functions need to take place on a background thread. I suggest you look into using AsyncTask to connect to your server and download the information you need. Here is a link to the android docs for AsyncTask. The basic idea is simple, any I/O functions go in the doInBackground() method. Any updating of the UI, e.g. setting the text on a TextView, takes place within onPostExecute(). Read the documentation and try to implement it. If you're still getting errors then, we can help you fix it.

Upvotes: 1

Goofy
Goofy

Reputation: 6128

Just try this example in the below link and try to do it in the same manner.

http://android-am.blogspot.in/2012/10/android-login-screen-by-connecting-to.html

Upvotes: 0

Olivier Lerone
Olivier Lerone

Reputation: 151

From what you say is in index.php,

    } else if ($tag == 'get_data_tag'){
  // Request type is get data

    $Channel_id = $_POST['Channel_id'];
    $Temp_value = $_POST['Temp_value'];

//check for the value for the specified channel
    $value = $db->getDataByChannelId($Channel_id, $Temp_value);

//this is where the data should be sent to the app
    $response_data["success"] = 1;
    $response_data["Channel_id"] = $value["Channel_id"];
    $response_data["Temp_value"] = $value["Temp_value"];
    echo json_encode($response_data);
}

PHP may be returning a parse error (because of the else without an if), and then the JSON library (in your Java/Android code) can't read something like "Parse Error: " as JSON which would lead it to throw that error.

Disregard this if you just omitted the other bits of code from your question.

Upvotes: 0

Related Questions