字符串无法转换为JSONObject?

时间:2012-05-03 11:06:56

标签: java php mysql json android-emulator

我是开发Android应用程序的初学者,这是我第一次从头开始编码,这就是为什么我觉得这对我很难。基本上,我只想摆脱错误,以便我现在可以开始在我的Android应用程序上显示SQL表。当我从mySQL表中获取数据时,我被这个错误禁止了。我在LogCat上得到这个:

Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
05-03 15:45:25.586: W/System.err(552): java.lang.NullPointerException
05-03 15:45:25.626: W/System.err(552):  at hs.stockapp.StocksTask.doInBackground(StocksTask.java:64)

如果我理解错误是正确的,那么我的StocksTask.java文件第64行就出错了。

第64行:if (json.getString(KEY_SUCCESS) != null)

StocksTask.java代码摘录:

protected Integer doInBackground(String... params)  {

    UserFunctions userFunction = new UserFunctions();
    JSONObject json = userFunction.findStocks();
    try {
        if (json.getString(KEY_SUCCESS) != null) {
            String res = json.getString(KEY_SUCCESS);

            if(Integer.parseInt(res) == 1){         
                DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext());
                JSONObject json_stocks = json.getJSONObject("stocks");
                Log.v("company_name", json_stocks.getString(STOCK_COMPANY_NAME));
                userFunction.findStocks();
                db.findStocks(json_stocks.getString(STOCK_COMPANY_NAME), json_stocks.getString(STOCK_SYMBOL), json_stocks.getString(STOCK_MCAP));                        

                responseCode = 1;
                // Close Screen
                //finish();
            }else{
                responseCode = 0;
                // Error 
            }
        }
    }
    catch (NullPointerException e) {
        e.printStackTrace();
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    return responseCode;
}

UserFunctions.java

 public JSONObject findStocks(){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", findstocks_tag));
    JSONObject json = jsonParser.getJSONFromUrl(stocksURL, params);
    // return json
    return json;
} 

的index.php

else if ($tag == 'stocks'){
    // request type is view stock list
    $stocks = $db->search_stocks();
    if ($stocks != false) {
        // stocks
        // echo json with success = 1
        $response["success"] = 1;
        $response["stocks"]["company_name"] = $stocks["name"];
        $response["stocks"]["com_sym"] = $stocks["com_sym"];
        $response["stocks"]["mkt_cap"] = $stocks["mkt_cap"];
        echo json_encode($response);
    } else {
        // no stocks found
        // echo json with error = 0
        $response["error"] = 1;
        $response["error_msg"] = "No stocks found";
        echo json_encode($response);
    }

} 

DB_functions.php

    public function search_stocks() {
    $result = mysql_query("SELECT company_name, com_sym, mkt_cap from stock ORDER BY com_sym") or die(mysql_error());
    $no_of_rows = mysql_num_rows($result);

    if ($no_of_rows > 0) {
        while($row=mysql_fetch_assoc($result)){
            $output[] = $row;
        }               
        print(json_encode($output)); 
        return $row;            
    return true;
    } else {
        // stocks not existed
        return false;
    }
mysql_close();  

}

产生search_stocks函数的结果

[{"company_name":"Asian Chorva","com_sym":"ASIA","mkt_cap":"4534356"},{"company_name":"Banko de Oro","com_sym":"BDO","mkt_cap":"54434"},{"company_name":"Bank of the Phil Island","com_sym":"BPI","mkt_cap":"5464554"},{"company_name":"College Hive In Boon","com_sym":"CHIB","mkt_cap":"5434654"},{"company_name":"Critical Heart Throb Racing","com_sym":"CHTR","mkt_cap":"53544564654"}]

我应该怎么做Line 64?非常感谢:)任何帮助将不胜感激。有关它的其他信息将非常有帮助。

2 个答案:

答案 0 :(得分:7)

错误消息显示:

  

值&lt;!java.lang.String类型的DOCTYPE无法转换为JSONObject

如果是<!DOCTYPE,那么它不是[{"company_name":…

您需要确切了解正在发出的请求以及服务器返回HTML文档的原因。

检查您的服务器日志。它可能是一个错误文档。

答案 1 :(得分:0)

在UserFunctions.java中正确显示您要显示的网址,例如"http://10.0.2.2/api/",其中api文件夹存储在www或htdocs文件夹中