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

时间:2018-03-09 17:55:33

标签: php android parsing

这里的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);

?>

我试图从php获取json_encode值,但值始终为null。在其他活动中,代码是有效的。但是当我为anoher活动创建时,我从json对象获得null错误。

这是一个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);

}

和错误

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

1 个答案:

答案 0 :(得分:0)

你没有打印任何东西:

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

这意味着,没有要解析的内容。正如错误消息所示:

  

字符0的输入结束

您尝试将0字节的数据解析为JSON对象,这是不可能的。

热门修复?

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