JSONParsing错误 - Android

时间:2014-08-19 16:34:25

标签: java php android json

错误日志:08-19 12:32:49.910:E / JSON解析器(1184):解析数据时出错org.json.JSONException:java.lang.String类型的值无法转换为JSONObject - String :{“成功”:0}

我的json字符串与“”相关,我无法弄清楚为什么......

JSONParser

package application.barattie;

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;

import android.util.Log;

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET method
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
            // defaultHttpClient
            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 {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString() + " ::: " + json);
    }

    // return JSON String
    return jObj;

}

}

manterMobile.php

    <?php

        $response = array();

        include_once 'classes/DB.php';
        include_once 'Objetos/Mobile.php';

        $mobile = new Mobile();
        $data = date('Y/m/d');

        $retorno = 0;

        //Verifica se a chamada foi feita pelo sistema
        if(isset($_POST['bmsa'])) {

            $mob_nome = $_POST['usu_nome'];
            $mob_email = $_POST['usu_email'];
            $mob_senha = $_POST['usu_senha'];
            $mob_ofertas = 1;

            //Adiciona um novo comentario
            if($_POST['bmsa'] == "usuario") {

                $sql = "SELECT * FROM `mobile` WHERE mob_usuemail = :mob_email;";
                $stmt = DB::prepare($sql);
                $stmt->bindParam(':mob_email', $mob_email);
                $stmt->execute();
                $verifica = $stmt->fetchColumn();
                if($verifica != "" || $verifica != NULL) {
                    $sql = "SELECT mob_id AS usu_wid FROM `mobile` WHERE mob_usuemail = :mob_email AND mob_ususenha = :mob_senha; ";

                    $stmt = DB::prepare($sql);
                    $stmt->bindParam(':mob_email', $mob_email);
                    $stmt->bindParam(':mob_senha', $mob_senha);
                    $stmt->execute();
                    $retornoId = $stmt->fetchColumn();

                    if($retornoId != "" && $retornoId != NULL) {
                        $sql = "SET autocommit=0; "
                            . "UPDATE `mobile` SET mob_usunome = :mob_nome WHERE mob_id = :mob_id;"
                            . "COMMIT";

                        $stmt = DB::prepare($sql);
                        $stmt->bindParam(':mob_nome', $mob_nome);
                        $stmt->bindParam(':mob_id', $retornoId);
                        $stmt->execute();

                        $retorno = 1;
                        $response["usu_wid"] = $retornoId;
                    } else {
                        $retorno = 0;
                    }
                } else {
                    $sql = "SET autocommit=0; INSERT INTO `mobile` (mob_usunome, mob_usuemail, mob_ususenha, mob_ofertas) VALUES (:mob_nome, :mob_email, :mob_senha, :mob_ofertas); "
                            . "SELECT mob_id AS usu_wid FROM `mobile` WHERE mob_usuemail = :mob_email AND mob_ususenha = :mob_senha; "
                            . "COMMIT";

                    $stmt = DB::prepare($sql);
                    $stmt->bindParam(':mob_nome', $mob_nome);
                    $stmt->bindParam(':mob_email', $mob_email);
                    $stmt->bindParam(':mob_senha', $mob_senha);
                    $stmt->bindParam(':mob_ofertas', $mob_ofertas);

                    if($stmt->execute()) {
                            $sql = "SELECT mob_id AS usu_wid FROM `mobile` WHERE mob_usuemail = :mob_email AND mob_ususenha = :mob_senha;";

                            $stmt = DB::prepare($sql);
                            $stmt->bindParam(':mob_email', $mob_email);
                            $stmt->bindParam(':mob_senha', $mob_senha);

                            $stmt->execute();

                            $wid = $stmt->fetchColumn();

                            $response["usu_wid"] = $wid;
                            $retorno = 1;
                    }
                }
            }

            $response["success"] = $retorno;
            echo json_encode($response);

        }

2 个答案:

答案 0 :(得分:0)

  1. 确保您的PHP页面保存为UTF-8

  2. 然后替换以下代码:

    $response["success"] = $retorno;
    echo json_encode($response);
    

    使用:

    header('Content-type: application/json');
    
    $json = json_encode($response, JSON_FORCE_OBJECT);
    
    echo($json);
    

答案 1 :(得分:0)

试着看一下:BOM randomly appears in JSON reply

尝试将PHP文件的编码更改为ANSI而不是UTF-8,并且BOM可能会消失。