【问题标题】:i am unable to decode json object in php我无法在 php 中解码 json 对象
【发布时间】:2013-07-16 09:49:48
【问题描述】:

这是我的 java 代码,它发出一个 http 请求并将 json 对象发送到 php 脚本。

login.java

    String username = jTextField1.getText();
    String password = jPasswordField1.getText();

    JSONObject obj = new JSONObject();

    obj.put("username", username);
    obj.put("password", password);

    //JSONArray list = new JSONArray();
    //list.add(username);
    //list.add(password);


    //obj.put("logindata", list);
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        HttpPost httppost = new HttpPost("http://localhost/kolitha/json_test/index.php");
        StringEntity se = new StringEntity("myjson" + obj.toString());
        httppost.setEntity(se);
        System.out.print(se);
        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-type", "application/json");
        System.out.println(obj.toString());

        //response = httpclient.execute(httppost);

        HttpGet httpget = new HttpGet("http://localhost/kolitha/json_test/index.php");
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        System.out.println(EntityUtils.toString(entity));



    } catch (Exception e) {
        e.printStackTrace();
        System.out.print("Cannot establish connection!");

    }

这是我的 index.php 脚本,我无法从 json 对象中提取用户名和密码。

index.php

$obj = json_decode(file_get_contents('php://input'));

$username=$obj->{'username'};
$password=$obj->{'password'};


$connect=mysql_connect('localhost', 'root', '');
IF (!$connect){
die ('Failed Connecting to Database: ' . mysql_error());}

$d = mysql_select_db("kolitha_json_test");
if(!$d){ echo "db not selected";}

$sql="SELECT * FROM login WHERE username='$username' AND password='$password' ";
$result=mysql_query($sql) or die (mysql_error());
$count=mysql_num_rows($result);

 // If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
echo "true";
}
else
 {
echo "false";
}
?>

【问题讨论】:

  • 你的 json 是什么样子的?
  • 我不明白你在问什么?
  • 能否请您发布var_dump($obj)的输出
  • 如果 json_decode() 失败,那么您提供的可能不是有效的 json。请回显 json 并将其添加到您的问题中
  • 你的json不是以StringEntity se = new StringEntity("myjson" + obj.toString());myjson为前缀吗?那会破坏json的伤口不是吗

标签: java php json


【解决方案1】:

好的,我发表了评论,但似乎没有人关注评论。错误在 Java 代码中

StringEntity se = new StringEntity("myjson" + obj.toString());

向名为myjson的json字符串添加前缀

【讨论】:

  • 你能解释一下应该怎么做吗?
【解决方案2】:

为什么同时使用 HttpPostHttpGet。 您正在向 HttpPost 请求添加字符串实体,而 不使用它..

而您使用 Httpget 而您的用户名和密码未设置(因此显然没有与 httpget 请求关联的 json 数据)

在此处进行更改

 response = httpclient.execute(httppost);//don comment this line

如果您使用的是 httppost,请在此处添加评论

//HttpGet httpget = new HttpGet("http://localhost/kolitha/json_test/index.php");
//response = httpclient.execute(httpget);

【讨论】:

  • 感谢 shanku.i 更正它。但它仍然无法正常工作。
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 2018-08-14
相关资源
最近更新 更多