I would like to connect my android app to a MySQL table I have on Hostinger. The layout is really simple: a simple TextView whose text has to be filled through a string with the setText.
I made sure I used the most up to date library (HttpURLConnection), that the PHP file on the server is well done, that I properly parse and that the app can connect to the internet through specification in the Manifest file.
Yet, when I open the app, I just see a black page and there is no error on the logcat. Any idea of what could it be?
This is my Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
while this is my MainActivity
public class MainActivity extends Activity { /** Called when the activity is first created. */
String line=null;
String [] stream_name;
HttpURLConnection urlConnection = null;
String value;
TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults(); //STRICT MODE ENABLED
resultView = (TextView) findViewById(R.id.result);
}
public void getData(){
String result = "";
InputStream isr = null;
try{
URL url = new URL ("http://31.170.165.234/public_html/AH_Bread2.php");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
isr = urlConnection.getInputStream();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
resultView.setText("Couldn't connect to database");
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"Price : "+json.getString("Price")+"
"+
"Weight : "+json.getInt("Weight")+"
"+
"Price/Weight : "+json.getString("P/W")+"
";
}
resultView.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}
And this is my PHP file on the server
<?php
$mysqli = NEW MySQL('marcodr.byethost7.com','xxxxxx','xxxxxx','xxxxxx');
$resultSet = $mysqli->query("SELECT * FROM TABLE 1 WHERE ID = 'xxxx'");
if($resultSet->num_rows != 0){
while($rows = $resultSet->fetch_assoc())
{
$ID = $rows['ID'];
$Price = $rows['Price'];
$Weight = $rows['Weight'];
$PW = $rows['PW'];
print(json_encode($rows);
}
}else{
echo "No results.";
}
?>
As you can see, my database is really simple: only four columns, properly named "ID", "Price", "Weight" and "P/W".
I would simply want to set the text of the TextView (identified through the FindViewById) to the string "s" defined by the end of the Java class, but it's not working despite all the checks! Any idea?