【发布时间】:2012-01-31 22:31:21
【问题描述】:
我正在尝试使用 PHP 从 mySQL 数据库中获取数据。这是我第一次真正尝试远程获取数据并使用 JSON。 php 文件运行正常,因为它在浏览器中输出为 JSON 字符串,并且我使用 JSONLint 对其进行了验证。所以,我不确定我在这里有什么问题。任何帮助将不胜感激
这就是 LogCat 所抛出的:
Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
threadid=9: thread exiting with uncaught exception (group=0x401dce20)
FATAL EXCEPTION: Thread-10
java.lang.NullPointerException
at com.andaero.test.JSON.JSONMain$1.run(JSONMain.java:39)
at java.lang.Thread.run(Thread.java:1020)
更新: 我按照 Mark 的要求从 php 文件中删除了 echo 方法。我认为这与“JSONArray a = json.getJSONArray("regulatory") 有关。我也尝试了其他所有人的方法,但没有占上风。
以下是课程:
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "regulatory";
JSONObject jArray = null;
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
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 + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
列表活动:
public class JSONMain extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
new Thread(new Runnable() {
public void run() {
JSONObject json = JSONfunctions
.getJSONfromURL("http://192.168.1.34/andaero/regulatory_list_ASC.php");
try {
JSONArray a = json.getJSONArray("regulatory");
for (int i = 0; i < a.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = a.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("label", e.getString("label"));
map.put("title", e.getString("title"));
map.put("caption", e.getString("description"));
map.put("dummy", e.getString("gotoURL"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}).start();
ListAdapter adapter = new SimpleAdapter(this, mylist,
R.layout.list_item, new String[] { "label", "title", "caption",
"dummy" }, new int[] { R.id.label, R.id.listTitle,
R.id.caption, R.id.dummy });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);
Toast.makeText(JSONMain.this,
"ID '" + o.get("id") + "' was clicked.",
Toast.LENGTH_SHORT).show();
}
});
}
}
已编辑: PHP:
<?php
//MySQL Database Connect
include 'andaerologin.php';
mysql_select_db("andaero");
$sql=mysql_query("select * from regulatory_list");
$output = new stdClass();
$output->regulatory = array();
while($row = mysql_fetch_assoc($sql)) {
$output->regulatory[] = $row;
}
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo (json_encode($output));
mysql_close();
?>
JSON 返回的数据**(部分 - 数据是机密的,JSON 已按上述验证!!)
[
{
_id: "**",
label: "**",
title: "**",
description: "**",
date: "**",
gotoURL: null,
intent: "QueryDisplay"
}, * additional rows.....>
]
}
【问题讨论】:
-
查看 JSON 部分会很有帮助。字符
<?xml可能不会被识别为有效的 JAVA 或 android JSON -
@Rafael 我添加了返回的 JSON 数据。请记住,JSON 已根据我的问题进行了验证。谢谢。
-
它是您的网络服务器,它为您提供垃圾。我已经测试了您检索 json 的方法,它工作正常。我什至创建了你必须用 mysql 制作 json 的 php。一切都很完美。您必须先修复您的服务器。否则我可以把我用于 json 的 URL 扔给你,你可以测试它。
-
检查此链接以获取正确的 mime 类型 stackoverflow.com/questions/477816/the-right-json-content-type
-
@Sergey Benner 我认为你可能是对的。我更改了他的 php 文件(见上文),但我找不到服务器出了什么问题。我也在网络上的另一台服务器上运行它,但我仍然得到同样的错误。唯一相同的变量是每个服务器都是 XAMPP v3.0.2。我现在不打算看看问题出在哪里。???再次验证返回的数组。