doutong2132 2014-02-17 20:42
浏览 175

解析数据的另一个错误org.json.JSONException:类型java.lang.String的值<!DOCTYPE无法转换为JSONObject

I'm pulling my hair out on this. I can write into a database with the same code (expect it's the INSERT INTO command) but this doesn't work.

PHP:

$id = $_POST['id'];
//$id = '000000000002';
$uri = $_POST['uri'];
//$uri='sdfgdsfg';
try {

    $stmt = $conn->prepare("UPDATE USERS SET PHOTO=? WHERE ID=?");
    $stmt->execute(array($uri, $id));
    $response["success"] = 1;
    } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    $response["success"] = 0;
}

echo json_encode($response);

JAVA:

try {
                        paramsC = new ArrayList<NameValuePair>();
                        paramsC.add(new BasicNameValuePair("id", userid));
                        paramsC.add(new BasicNameValuePair("uri", "hello"));

                        JSONParser jsonParser; jsonParser = new JSONParser();
                        JSONObject json = jsonParser.makeHttpRequest(set_profile_photo_uri, "POST", paramsC);

                        try {
                            int success = json.getInt("success"); //error points to this line
                            if (success == 1) {
                                // successfully updated
                                Log.d("SIGNUP", "success");
                            } else {
                                Log.d("SIGNUP", "fail");
                            }
                        }catch(JSONException e){
                            //Log.e("log_tag", "Error parsing data "+e.toString());
                            //Log.e("log_tag", "Failed data was:
");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

I get an error:

02-17 21:37:45.104: E/JSON Parser(21276): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-17 21:37:45.104: W/System.err(21276): java.lang.NullPointerException
02-17 21:37:45.124: W/System.err(21276):    at com.example.myapp.TabFragment1$8.onClick(TabFragment1.java:171)

However, this code works, but WHY?:

try {
        $stmt = $conn->prepare("INSERT INTO USERS (USERNAME, PASSWORD, NAME, EMAIL, GENDER, DATEOFREG_PHONE, DATEOFREG_SV) VALUES
        (:username, :password, :name, :email, :gender, :dateofreg_phone, :dateofreg_sv)");
            $query_params = array( 
                ':username' => $username,
                ':password' => $password,
                ':name' => $name,
                ':email' => $email,
                ':gender' => $gender,
                ':dateofreg_phone' => $dateofreg_phone,
                ':dateofreg_sv' => $dateofreg_sv
            );  
        $stmt->execute($query_params); 
        $response["success"] = 1;
        } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
        $response["success"] = 0;
    }

    echo json_encode($response);

The PHP code works fine when I enter it into the browser.

  • 写回答

1条回答 默认 最新

  • doukuibi9631 2014-02-17 21:28
    关注

    Making an answer from comments to close this question: Make sure you send a query to a right URL.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序