为什么JSON返回null?

时间:2016-01-21 06:44:06

标签: php android mysql json

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

这是图片

enter image description here

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

我想要的是消息应该是成功还是失败。我不知道什么是代码。这是我的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()'它没有给我任何错误。我不明白我的错在哪里。

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

编辑这是我的java代码进行调用并接收JSON。 让我详细说明一下这个问题。在下面的代码'line'中,我用来将响应传递给json是null。

以下是我在控制台中尝试的内容:

  1. 我试过'reader.toString()',什么都没给我
  2. 2.我试过'is.toString()',奇怪地在控制台中给了我'成功'的信息。

    所以问题似乎在这个代码中。我很抱歉我说问题出在我的PHP代码上。我是初学者所以请好好理解我。请帮忙。

    JSONparser

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

2 个答案:

答案 0 :(得分:0)

你得到了三个回声

 echo phpversion();
    echo json_last_error_msg();

    echo json_encode($response);

var_dump( $Mobile);

答案 1 :(得分:0)

这里没问题,

User.first.email

删除除json_encode之外的所有内容,你实际上得到了你想要的内容