我想从使用PHP的mySQL数据库中获取数据。这是我第一次使用JSON远程获取数据&的真正尝试。 php文件运行正常,因为它在浏览器中输出为JSON字符串,我使用JSONLint对其进行了修改。所以,我不确定我在这里有什么问题。任何帮助将不胜感激来自PHP LogCat的JSON:字符串无法转换为JSONObject
这是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)
UPDATE:我从PHP文件中删除的回声法马克要求。我认为这与“JSONArray A = json.getJSONArray(”监管“),我也尝试过其他人的,没有办法为准做
这里是类:。
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 – 2012-01-31 22:43:49
@Rafael我添加了返回的JSON数据。请记住,根据我的问题验证了JSON。日Thnx。 – CelticParser 2012-01-31 23:03:16
这是你的网络服务器,它给你垃圾。我已经测试了你的方法来检索JSON并且工作正常。我甚至创建了PHP,你必须从MySQL中创建JSON。一切都很完美。你必须先修复你的服务器。否则,我可以将您用于json的URL引向您,并且可以对其进行测试。 – 2012-02-01 13:25:46