Java 解析JSON数据时出现问题
我已经寻找了一天的解决方案,但找不到一个适合我的情况。很抱歉,我是JSON(自学程序员)的新手,我不知道应该只发布什么课程,所以我会把我所有的东西都放进去。我从LogCat获得以下错误:Java 解析JSON数据时出现问题,java,php,android,json,Java,Php,Android,Json,我已经寻找了一天的解决方案,但找不到一个适合我的情况。很抱歉,我是JSON(自学程序员)的新手,我不知道应该只发布什么课程,所以我会把我所有的东西都放进去。我从LogCat获得以下错误: Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONArray 以下是我的JSON解析器类: package com.example.mytr
Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONArray
以下是我的JSON解析器类:
package com.example.mytravelbuddy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
package com.example.mytravelbuddy;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.client.utils.URLEncodedUtils;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.util.Log;
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
}
任何帮助都会非常有用,可能会解释我出现此错误的原因,这样我就不会重复此错误了。这可能会导致问题
mysql_connect($server, $user, $pass);
$db_found = mysql_select_db($database);
if($db_found){
echo"DB Found<br>";
}else{
echo"DB NOT Found<br>";
}
echo"Connection Established<br>";
将其放在脚本的顶部
编辑:
我刚刚尝试了你的代码,我可以毫无错误地解析JSON
很可能您的PHP服务器出于某种原因正在发送XML。您需要再次检查该脚本。
甚至可能是错误报告弄乱了编码的json,记录任何错误,但只回显json
希望这能有所帮助。php页面正在回显其他字符串,而不是json(仅) 我强烈建议使用来解析JSON响应 对于上面的示例,该类如下所示:
public class ItemList {
List<Item> items;
}
然后,解析变得非常简单。将gson-2.2.4.jar
放入您的libs文件夹,然后执行以下操作:
Gson GSON = new Gson();
ItemList itemList = GSON.fromJson(json_string_here, ItemList.class);
就这样。您有一个从JSON解析的第一类java对象。简单且功能强大。您似乎收到的是XML而不是JSON。而且此
方法==“GET”
是错误的。比较字符串时使用.equals
。是的,我知道这一部分。我不知道的是如何修复它?我的数据库有问题吗?@SotiriosDelimanolis这是一个预先制作的解析器类。。。谢谢你指出:)请。它们不再被维护,并被正式弃用。相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,。我更改了这些行,我的应用程序仍然会因相同的错误而崩溃。@user2904544我只是尝试了一下您的代码,它的工作非常完美,您必须对PHP进行故障排除,它没有正确地回显JSON。问题可能是因为我返回了2个浮点数(经度和纬度)从我的表格?@user2904544不,因为你在引号中有它,所以JSON会将它作为字符串处理,你应该真正调试PHP,尝试注释所有内容,你只需硬编码JSON数据,然后回显,然后一点一点地取消对代码的注释,您知道基本的调试。如果使用Gson,我就不需要解析器类了?您仍然需要从提供JSON的任何服务器下载JSON,但您不需要使用jObj=new JSONObject(JSON)
anymore在解析前打印json字符串。
mysql_connect($server, $user, $pass);
$db_found = mysql_select_db($database);
if($db_found){
echo"DB Found<br>";
}else{
echo"DB NOT Found<br>";
}
echo"Connection Established<br>";
mysql_connect($server, $user, $pass);
$db_found = mysql_select_db($database);
if($db_found){
error_log("DB Found<br>");
}else{
error_log("DB NOT Found<br>)";
}
error_log("Connection Established<br>");
header("Content-Type: application/json");
public class ItemList {
List<Item> items;
}
public class Item {
private String ID;
private String Location;
private String Description;
private String Longitude;
private String Latitude;
private String Time;
}
Gson GSON = new Gson();
ItemList itemList = GSON.fromJson(json_string_here, ItemList.class);