0

我在解析从服务器获得的数据时遇到错误。我试图通过向外部数据库(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"‌​
}
4

0 回答 0