I am trying to write an app that will connect to a database, extract data, and display that data on the phone. I am running into a problem though.
I connect to the database using:
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://74.208.131.62/Androidtest/testscript.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.d("Connection Success","Connected To Database");
} catch(Exception e) {
}
test.php is an sql which will return values: Item_ID(auto_inc_int), OtherTerms(String).
Then, I convert it to a string:
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
is.close();
result = sb.toString();
}
Then I use this part, which I don't understand, I just copied it from the internet:
int Item_id;
String Item_name;
String populate = new String();
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for(int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
Item_id = json_data.getInt("id");
Item_name = json_data.getString("item");
}
...
However, I get the message "No Item found". I tried changing the name of the database, but I didn't get "Error in Connection" in LogCat, rather "No Item found".
I don't know if my app is successfully connecting to the database or not. I need to insert the result into a ListView object. What am I doing wrong?
(Also, happy holidays to you all!)