【问题标题】:cannot retrieve json_encode value from php file into android无法将 json_encode 值从 php 文件检索到 android
【发布时间】:2014-10-17 07:52:57
【问题描述】:
 <?php 

 require 'connection.php';

 $response = array();

 $response_array = array();

 $insert_query=0;

 if(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["phone"]))
{
 $username = $_POST["username"];
 $password = $_POST["password"];
 $phone = $_POST["phone"];

 $check_existing_user = mysql_query("select * from eh_users where username like '".$username."'");



if(mysql_num_rows($check_existing_user)==0)
    {


 $insert_query = mysql_query("INSERT INTO eh_users(username, password, phone)           VALUES('$username','$password','$phone')");


 if($insert_query==1)
    {
        $response["success"]=1;

        $response["message"]="Insert Query Successful";

        file_put_contents("myFile.txt", json_encode($response));

        echo json_encode($response);

    }   

else
    {
        $response["success"]=0;

        $response["message"]="Error : Query not successful";

        echo json_encode($response);
    }

}
}

?>

受保护的字符串 doInBackground(String...params) { // TODO 自动生成的方法存根

        try
        {
            String url="http://www.iloveexpressions.com/eh/signUp.php";

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost(url);
            nameValuePairs = new ArrayList<NameValuePair>(3);

            Log.d("Parameters ", username+" "+password+" "+phone);

            nameValuePairs.add(new BasicNameValuePair("username", username.trim()));
            nameValuePairs.add(new BasicNameValuePair("password", password.trim()));
            nameValuePairs.add(new BasicNameValuePair("phone", phone.trim()));

            Log.d("name value pairs", nameValuePairs.toString());

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response = httpclient.execute(httppost);

            JSONObject json = jsonParser.getJSONFromUrl(url);

            int success = json.getInt("success");

            //JSONArray jArray = json.getJSONArray("success");

            Log.d("Json object : ",success+"");

            /*for(int i = 0; i < jArray.length(); i++ )
            {
                JSONObject c = jArray.getJSONObject(i);

                String success = c.getString("success");

                String message = c.getString("message");

                Log.d("Sucess : ", success);

                Log.d("Message : ", message);
            }*/


        }

文本文件包含正确的内容 {"success":1,"message":"Insert Query Success"}

但我无法在 android 中检索它

【问题讨论】:

  • 对于发布问题的非结构化方式感到抱歉。第一次
  • 您可以尝试打印 JSONObject json 的结果是什么吗?有什么错误吗?
  • 检查响应,是否为空?
  • 调试一下,看看json对象是不是你所期望的。
  • 当我打印响应时,日志猫会显示这个 org.apache.http.message.BasicHttpResponse@b239def8

标签: php android json


【解决方案1】:

首先使用&lt;new HttpPost.(url) 中的url 登录,然后使用httpclient.execute(httppost) 并再次使用getJSONFromUrl(url); 中的url 是没有意义的。相反,您应该从现有响应中读取 json。

HttpResponse response = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

StringBuilder sb = new StringBuilder();

String line;
while ((line = reader.readLine()) != null) 
    {
    sb = sb.append(line);
    }

String jsonText =  sb.toString();

【讨论】:

  • 你说的有道理..所以我必须使用 Httpresponse 响应?
  • 您提供的解决方案运行良好,但使用您的方法,我在 android 上获得的数据是一个字符串。如果我想要我的 php 文件中的响应数组作为 json 对象,我该怎么办?
  • 否 php 脚本不包含 json 对象或数组。它包含 php 变量,它们以 json 编码文本的形式回显。因此,您会收到字符串中的 json 文本。但是有了 json 文本,您现在需要一个 json 类来处理该 json 文本并检索对象。
【解决方案2】:

在我的情况下..我使用的是 Http 方法,然后是来自 JSONParser 类的 getjsonfromURL,该类也有用于 get 和 post 的 Http 方法

我认为有两个调用,因此没有对象被返回

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2020-07-24
    相关资源
    最近更新 更多