Hi i have a problem with display data from my database on my app
That's part of Java file
@Override
protected String doInBackground(String... params)
{
String result ="";
String host = "http://192.168.0.12/LoginRegister/workelectricallist.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 (Exception e)
{
return new String("There exception: " + e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result)
{
try
{
Toast.makeText(getApplicationContext(),"1 enter", Toast.LENGTH_SHORT).show();
JSONObject jsonResult = new JSONObject(result);
int success = jsonResult.getInt("success");
if (success == 1)
{
Toast.makeText(getApplicationContext(),"2 enter", Toast.LENGTH_SHORT).show();
JSONArray ad = jsonResult.getJSONArray("ad");
for(int i=0; i < ad.length(); i++)
{
JSONObject object = ad.getJSONObject(i);
int id = object.getInt("id");
String user_name = object.getString("user_name");
String NameAd = object.getString("NameAd");
double Content = object.getDouble("Content");
String TypeOfAd = object.getString("TypeOfAd");
String line = id + "-" + user_name + "-" + NameAd + "-" + Content + "-" + TypeOfAd;
adapter.add(line);
}
}
else
{
Toast.makeText(getApplicationContext(),"No Ad to display", Toast.LENGTH_SHORT).show();
}
}
catch (JSONException e)
{
Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
php file take data from MySQL data base, i can see array of data on a broswer but on my app, i can see only "1 enter" and "no value for a success" Like it didn't go to if, but there is no "No ad to display" so it didnt go to else
That's php file
<?php
$host ='localhost';
$user ='root';
$pwd ='';
$db ='loginregister';
$conn = mysqli_connect($host, $user, $pwd, $db);
if(!$conn)
{
die("Error in connection" . $mysqli_connect_error());
}
$response = array();
$sql_query = "select *from ad";
$result = mysqli_query($conn, $sql_query);
if(mysqli_num_rows($result) > 0)
{
$response['succes'] = 1;
$ad = array();
while ($row = mysqli_fetch_assoc($result))
{
array_push($ad, $row);
}
$response['ad'] = $ad;
}
else
{
$response['succes']=0;
$response['succes']='no data';
}
echo json_encode($response);
mysqli_close($conn);
?>