gpt4 book ai didi

java - 解析数据时出错 org.json.JSONException : Value of type java. lang.String 无法转换为 JSONArray

转载 作者:行者123 更新时间:2023-12-01 04:31:47 25 4
gpt4 key购买 nike

我正在从我的 Android 应用程序连接到外部数据库。它是使用 PHP JSON 的 MS SQL Server。我已成功连接到数据库并在浏览器中获取结果。来到 android 时,我没有得到任何结果,它显示 Error Parsing Data org.json.JSONException: Value of type java.lang.String无法转换为 JSONArray。下面是我的代码。

getDatabase.php

<?php header('content-type: application/json; charset=utf-8');
$myServer = "example";
$myUser = "user";
$myPass = "pq";
$myDB = "dbname";


$conn = new COM ("ADODB.Connection")
or die("Cannot start ADO");


$connStr ="PROVIDER=SQLOLEDB;SERVER=".$myServer.";
UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
$conn->open($connStr);


$query = "SELECT parkingtitle,address FROM parkd_dailyparkingslots";


$rs = $conn->execute($query);

$num_columns = $rs->Fields->Count();

for ($i=0; $i < $num_columns; $i++) {
$fld[$i] = $rs->Fields($i);
}


while (!$rs->EOF)
{

for ($i=0; $i < $num_columns; $i++) {
print(json_encode($fld[$i]->value));
}

$rs->MoveNext(); //move on to the next record
}


$rs->Close();
$conn->Close();

$rs = null;
$conn = null;
?>

数据库 Activity

public class Database extends Activity {
/** Called when the activity is first created. */

TextView resultView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
StrictMode.enableDefaults(); // STRICT MODE ENABLED
resultView = (TextView) findViewById(R.id.result);
getData();
}

public void getData() {
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.example.com/getDatabase.php");

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
resultView.setText("Couldnt connect to database");
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
isr, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();

result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}

// parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
String json = jArray.getString(i); // JSONObject
// json = jArray.getJSONObject(i);
JSONObject jObj = new JSONObject(json);

s = s + "Name: " + jObj.getString("parkingtitle") + "\n\n"
+ "Address: " + jObj.getString("address") + "\n\n";
}
resultView.setText(s);

} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data " + e.toString());
}
}
}

我在谷歌中搜索并检查了所有帖子,但没有得到任何解决方案。请帮我解决这个问题。提前致谢。

最佳答案

尝试使用:

JSONObject jObj = jArray.getJSONObject(i);

而不是:

jArray.getString(i); // JSONObject // json = jArray.getJSONObject(i); 
JSONObject jObj = new JSONObject(json);

关于java - 解析数据时出错 org.json.JSONException : Value of type java. lang.String 无法转换为 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17882914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com