【问题标题】:Why is JSON returning null?为什么 JSON 返回 null?
【发布时间】:2016-01-21 06:44:06
【问题描述】:

我正在制作一个处理注册的应用程序。我使用 PHP MYSQL。当我注册时,输入的详细信息被正确插入到表中,但是当回显响应时,它返回 NULL 和消息。

这是图片

在应用程序中,这个 NULL 是我作为 json 提要得到的,因此我无法继续进行。

我想要的是消息应该是成功还是失败。我不知道代码有什么问题。这是我的 PHP 代码:

<?php
include_once './DbConnect.php';
function createNewPrediction() {

     $response = array();
    $Name = $_POST["Name"];
    $College =  $_POST["College"];
    $Mobile =  ($_POST["Mobile_no_"]);
    var_dump( $Mobile);

    $Email =  $_POST["Email"];
            $db = new DbConnect();

   // mysql query
   mysql_query('SET CHARACTER SET utf8');
    $query = "INSERT INTO Register(Name,College,Mobile,Email)     VALUES('{$Name}','{$College}','{$Mobile}','{$Email}')";
    $result = mysql_query($query) or die(mysql_error());
    if ($result) {
        $response["error"] = false;
        $response["message"] = "Registered Successfully!!";
    } else {
        $response["error"] = true;
        $response["message"] = "Registration unsuccessfull!!";
    }
   // echo json response
   echo phpversion();
   echo json_last_error_msg();

echo json_encode($response);
}
createNewPrediction();
?>

如您所见,我尝试了 'json_last_error_msg()' 它没有给我任何错误。我不明白我的错在哪里。

希望这些材料足以评估问题。请帮帮我?

EDIT 这是我发出调用和接收 JSON 的 java 代码。 让我详细说明一下这个问题。在下面的代码中,我用来将响应传递给 json 的“行”为空。

这是我在控制台中尝试过的:

  1. 我试过'reader.toString()',什么也没给我

2.我试过'is.toString()',奇怪的是在控制台给我消息'success'。

所以问题似乎出在这段代码中。我很抱歉我说问题出在我的 php 代码上。我是初学者,所以请理解我。请帮助。

JSON解析器

package com.defcomdevs.invento16;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

 /**
 * Created by midhun on 18/11/15.
*/
public class JSONParser {

static  InputStream is= null;  //input stream object to hold incoming data
static  JSONObject obj=null;
static String json="";

//constructor
public JSONParser(){

}


//functionn to get json from URL
//by making HTTP POST or GET methods
public JSONObject makeHTTPRequest(String url, String method,List<NameValuePair> params){

    //making HTTP request
    try{

        //check for request method
        if (method== "POST"){

            //request method is post
            //call default http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is= httpEntity.getContent();
        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }
    }catch (UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (ClientProtocolException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    try {
        BufferedReader reader= new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line= null;
        while((line = reader.readLine())!=null){
            sb.append(line +"\n");
        }
        is.close();
        json = sb.toString();

    }catch (Exception e){
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    //try parse the string to A JSON object

    try{
        obj=new JSONObject(json);
    }catch (JSONException e){
        e.printStackTrace();
    }
    return obj;
}

public static  String returnJSON(){
    return json;
}
}

【问题讨论】:

  • var_dump( $Mobile);.....echo phpversion();......echo json_last_error_msg()......它们都产生输出。因此无效的json 输出......
  • 删除var_dump( $Mobile);

标签: php android mysql json


【解决方案1】:

你把三个回声拿出来

 echo phpversion();
    echo json_last_error_msg();

    echo json_encode($response);

var_dump( $Mobile);

【讨论】:

    【解决方案2】:

    这里没有问题,

    var_dump -> NULL
    echo phpversion(); -> 5.6.4 ...
    echo json_last_error_msg();->No error
    echo json_encode($response);->{"error":false,"message":"Registered Successfully!!"}
    

    删除除 json_encode 之外的所有内容,您实际上得到的正是您想要的

    【讨论】:

    • 感谢您提供的信息。我删除了将 null 返回给我的应用程序的“var_dump”想法。但我再次运行它,发现它仍然只接收 null。所以我正在上传上面的代码,请仔细阅读。
    猜你喜欢
    • 2020-07-10
    • 2016-12-28
    • 2022-12-26
    • 1970-01-01
    • 2015-11-25
    • 2015-07-04
    • 2019-09-29
    • 2015-02-25
    • 2015-10-21
    相关资源
    最近更新 更多