简体   繁体   English

无法将php中的json_encode值解析为android

[英]Cannot parse json_encode value from php into android

here's php script 这是PHP脚本

<?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. 我试图从php获取json_encode值,但值始终为null。 in other activity the code is worked. 在其他活动中,代码有效。 but when i create for anoher activity i get null error from json object. 但是当我为其他活动创建时,我从json对象中得到了空错误。

and this a android script 这是一个Android脚本

    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. 请帮助,如果您已经解决了这样的问题,谢谢您的帮助。

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 输入在字符0处结束

You tried to parse 0 bytes of data as a JSON object, which is not possible. 您试图将0字节的数据解析为JSON对象,这是不可能的。

Hot to fix? 热修复?

header('Content-Type: application/json');
echo json_encode(array('email_template' => $mailcontent));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM