错误解析数据无法将字符串转换为JSON-JSONException

时间:2012-05-23 00:49:22

标签: java php android mysql json

我在解析从服务器获取的数据时遇到错误。 我试图通过向外部数据库(MySQL)发送一些值来更新外部数据库。更新数据库后,我正在尝试获取更新的值,并在内部SQLite数据库上执行更新。

我发布了此问题的相关代码。另外,我已经在profile.java类中标记了我收到错误的地方,我正在尝试检索JSON。

当我在注册用户后检索数据时,我没有得到与解析数据相关的错误。这只是更新。

此外,外部数据库没有得到更新。

是的,在发布此问题之前,我已经做了足够的研究。我没有找到任何人或任何有同样问题的话题,这就是我发布这个问题的原因。

更新:我得到它的工作问题是我在index.php中将标记设置为变量而不是值。我正在做tag =“$ profile”而不是tag ='profile'。目前我已成功更新了一列。一旦我成功更新了所有列,我将再次更新这篇文章。谢谢大家的宝贵意见。

以下是profile.java的代码

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.b_profileSave:
        String email = email_et.getText().toString();
        String name = username_tt.getText().toString();
        String fullname = fullname_et.getText().toString();
        String dob = dob_et.getText().toString();
        String phone = phone_et.getText().toString();
        String address = address_et.getText().toString();
        String city = city_et.getText().toString();
        String state = state_et.getText().toString();
        String country = country_et.getText().toString();
        String zipcode = zipcode_et.getText().toString();
        UserFunctions userFunction = new UserFunctions();
//IM GETTING ERROR AT THIS POINT
        JSONObject json = userFunction.updateProfile(email, fullname, dob,
                phone, address, city, state, country, zipcode);
        System.out.println(json);
                    ....}

下面是我的UserFunction类的代码,该类具有profile方法,该方法负责从服务器发送数据和检索JSON:

public JSONObject updateProfile(String email, String fullname, String dob,
        String phone, String address, String city, String state,
        String country, String zipcode) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", profile_tag));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("full_name", fullname));
    params.add(new BasicNameValuePair("birth_date", dob));
    params.add(new BasicNameValuePair("phone", phone));
    params.add(new BasicNameValuePair("address", address));
    params.add(new BasicNameValuePair("city", city));
    params.add(new BasicNameValuePair("state", state));
    params.add(new BasicNameValuePair("country", country));
    params.add(new BasicNameValuePair("zip", zipcode));
    JSONObject json = jsonParser.getJSONFromUrl(profileURL, params);
    System.out.println(params);
    Log.e("JSON", json.toString());
    return json;
}

以下是我的JSONParse.java类的代码,它负责解析数据:

public class JSONParser {

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

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // 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();

    } 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);
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } 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());
    }

    // return JSON String
    return jObj;

}
}

下面是我的index.php的代码,POST和GET数据

else if ($tag == '$profile'){
    $email = $_POST['email'];
    $fullname = $_POST['full_name'];
    $dob = $_POST['birth_date'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $country = $_POST['country'];
    $zipcode = $_POST['zip'];
    $user = $db->profile($name, $fullname, $dob, $phone, $address, $city, $state, $country, $zipcode);
        if($user){
            $response["success"] = 1;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            $response["user"]["full_name"] = $user["full_name"];
            $response["user"]["birth_date"] = $user["birth_date"];
            $response["user"]["phone"] = $user["phone"];
            $response["user"]["address"] = $user["address"];
            $response["user"]["city"] = $user["city"];
            $response["user"]["state"] = $user["state"];
            $response["user"]["country"] = $user["country"];
            $response["user"]["zip"] = $user["zip"];
        echo json_encode($response);
        }else{
            $response["error"] =1;
            $response["error_msg"] = "Error updating profile";
            echo json_encode($response);
            } 
            } else {
    echo "Invalid Request";
}

下面是DB_Functions.php的代码,它在数据库上执行查询:

public function profile($email, $fullname, $dob, $phone, $address, $city, $state, $country, $zipcode){
    $result = mysql_query("UPDATE users SET updated_at = NOW(), full_name = '$fullname', birth_date = '$dob', phone = '$phone', address = '$address', city = '$city', state = '$state', country = '$country', zip = '$zipcode' WHERE email = '$email'") or die(mysql_error());
    if($result){
        $r = mysql_query("SELECT * FROM users WHERE email = '$email'");
        return mysql_fetch_array($r); 
    }else{
        return false;}
}

我的logcat输出:

05-23 03:01:03.938: E/JSON(393): Invalid Request
05-23 03:01:03.938: E/JSON Parser(393): Error parsing data org.json.JSONException: Value Invalid of type java.lang.String cannot be converted to JSONObject


E/JSON(393): 

{
    "uid": "4fb32016d06af1.89812745",
    "error": 0,
    "user": {
        "zip": null,
        "phone": null,
        "upda‌​ted_at": null,
        "birth_date": null,
        "address": null,
        "email": "vvp@gmail.com",
        "name": "vik‌​ing",
        "state": null,
        "created_at": "2012-05-15 20:33:42",
        "country": null,
        "city": null,
        "full_name": null
    },
    "success": 1,
    "tag": "login"‌​
}

0 个答案:

没有答案