0

here's php script

<?php
include_once "koneksi.php";
class usr
{
}

$email = $_GET['Email'];
//$email = "adm.topgrowth@gmail.com";

$checkuser = mysqli_query($koneksi, "SELECT * FROM users_profile WHERE Email='" . $email . "'");
if ($checkuser) {

    $getnama = mysqli_fetch_array($checkuser);

    $nama = $getnama["FullName"];
}
//echo $email;
$random_number = intval("0" . rand(1, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9));

$mail_temp = mysqli_query($koneksi, "SELECT * FROM mail_template WHERE id= 1");


$mailcontent = array();
if ($mail_temp) {

    $result       = mysqli_fetch_array($mail_temp);
    $subject_mail = $result["SubjectMail"];
    $body_mail    = $result["BodyMail"];
    $bodycontent  = preg_replace(array(
        '/{fullname}/',
        '/{link}/'
    ), array(
        $nama,
        $random_number
    ), $body_mail);


}
array_push($mailcontent, array(
    'code' => $random_number,
    'BodyMail' => $bodycontent,
    'subject' => $subject_mail
));

json_encode(array(
    'email_template' => $mailcontent
));
//echo $bodycontent;

mysqli_close($koneksi);

?>

im trying to get json_encode values from php, but the values is always null. in other activity the code is worked. but when i create for anoher activity i get null error from json object.

and this a android script

    public void getmail(String email){

    String Email = email;

    RequestQueue requestqueue = Volley.newRequestQueue(EmailHandler.this);
    StringRequest stringReq = new StringRequest(Request.Method.GET,
            Database.verifikasiUrl+Email, new Response.Listener<String>() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onResponse(String response) {
            Log.d("response", response);
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("email_template");
                for (int a = 0; a < jsonArray.length(); a++) {
                    JSONObject json = jsonArray.getJSONObject(a);
                    HashMap<String, String> map = new HashMap<>();
                    map.put("code", json.getString("code"));
                    map.put("BodyMail", json.getString("BodyMail"));
                    map.put("subject", json.getString("subject"));

                    ArrayList<HashMap<String,String>> verifikasi = new ArrayList<>();
                    verifikasi.add(map);
                    code = verifikasi.get(0).get("code");
                    subject = verifikasi.get(0).get("subject");
                    body_mail = verifikasi.get(0).get("BodyMail");

                    sendMail();

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    requestqueue.add(stringReq);

}

and the error

03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err: org.json.JSONException: End of input at character 0 of 
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:156)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at appuser.com.signalsbuddy.ServiceHandler.EmailHandler$1.onResponse(EmailHandler.java:75)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at appuser.com.signalsbuddy.ServiceHandler.EmailHandler$1.onResponse(EmailHandler.java:69)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-10 00:30:18.090 21147-21147/appuser.com.signalsbuddy W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)

pliss help if you already solved problem like this, thank's for your help.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • [Little Bobby](http://bobby-tables.com/) says [**your script is at risk for SQL Injection Attacks**](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – rollstuhlfahrer Mar 09 '18 at 17:57
  • @ADM Not an Issue with the Android Code – rollstuhlfahrer Mar 09 '18 at 17:59

1 Answers1

0

You are not printing anything:

json_encode(array('email_template' => $mailcontent));

This means, there is no content to parse. As the error message says:

End of input at character 0

You tried to parse 0 bytes of data as a JSON object, which is not possible.

Hot to fix?

header('Content-Type: application/json');
echo json_encode(array('email_template' => $mailcontent));
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • If the answer solves your problem, [please accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – rollstuhlfahrer Mar 10 '18 at 09:41
  • sorry sir, still getting error with End of input character 0. i try to printout json_encode in browser but the value doesn't appear –  Mar 10 '18 at 10:13
  • i know where's the problem sir, when i put the other variable into json_encode it can printed in browser. but for $mailcontent variable, it cannot. so the problem is in $mailcontent as array. can yo give me solution for this sir?? –  Mar 10 '18 at 10:42