0

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);

?>

1 Answers1

0

Using a local service you also need to see your development environment. If the app runs on the same machines where the site is hosted there should be no problem. If, on the other hand, the app is used externally, the error may be caused by the http request not being able to get a response because it does not know who to ask for the response.

I invite you to analyze the try and catch since there is the error "no value for a success". It is very likely that the result string do not get readed correctly or is not in a json format and because of that sends you the exception.

By googling "JSONException no value for" it possible to find extra information about this kind of issue like Android - JSONException No value for

DoktorSAS
  • 21
  • 6