简体   繁体   English

为什么JSON返回null?

[英]Why is JSON returning null?

I 'm making an application that handles registration.I use PHP MYSQL.When i register ,the details entered are correctly inserted into the table but when echoed response it returns NULL with the message. 我正在制作一个用于处理注册的应用程序。我使用PHP MYSQL。注册时,输入的详细信息正确插入到表中,但在响应时它返回NULL和消息。

here is the picture 这是图片

在此处输入图片说明

When in application this NULL is what i get as json feed thus i can't proceed further. 在应用程序中时,此NULL是我作为json feed获得的,因此我无法继续进行。

what i want is that the message should be either success or failure.I don't know whats wong with the code.Here is my PHP code: 我想要的是消息应该是成功还是失败。我不知道代码会是什么。这是我的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();
?>

As you can see i have tried 'json_last_error_msg()' it gives me no error.I don't understand where lies my fault. 如您所见,我已经尝试过'json_last_error_msg()'它没有错误。我不明白我的错在哪里。

Hope these materials are enough for the assessment of the question.Please help me out? 希望这些材料足以评估问题。请帮帮我吗?

EDIT here is my java code making the calls and receiving the JSON. 这里的编辑是我的java代码进行调用并接收JSON。 Let me elaborate the problem.In the below code 'line' that i'm using to pass the response to json is null. 让我详细说明问题。在下面的代码中,我用于将响应传递给json的``行''为null。

Here is what i have tried in console: 这是我在控制台中尝试过的内容:

  1. I have tried 'reader.toString()', gave me nothing 我试过了'reader.toString()',什么也没给我

2.I have tried 'is.toString()',strangely gave me message 'success' in console. 2.我尝试过“ is.toString()”,奇怪地在控制台中给了我消息“成功”。

So problem seems to be in this code.I 'm very sorry that i said that the problem was with my php code.I 'ma beginner so please kindly understand me.Please help. 因此问题似乎出在此代码中。非常抱歉,我说问题出在我的php代码中。我是初学者,请您理解我。请提供帮助。

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

you get the out put the three echo's 你拿出三个回声

 echo phpversion();
    echo json_last_error_msg();

    echo json_encode($response);

and var_dump( $Mobile); var_dump( $Mobile);

You have no problem here, 你没问题

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

Remove everything but json_encode and you actually get exactly what you want 删除除json_encode之外的所有内容,您实际上将获得所需的结果

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

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