Android-解析数据org.json.JSONException:Value<;时出错;?无法将java.lang.String类型的xml转换为JSONArray
我不知道发生了什么,我的代码中没有错误,但我的应用程序中的ListView无法显示,如果有少于我的代码?这是我的项目,无法显示ListView JSONParser.javaAndroid-解析数据org.json.JSONException:Value<;时出错;?无法将java.lang.String类型的xml转换为JSONArray,java,php,android,json,android-listview,Java,Php,Android,Json,Android Listview,我不知道发生了什么,我的代码中没有错误,但我的应用程序中的ListView无法显示,如果有少于我的代码?这是我的项目,无法显示ListView JSONParser.java 导入java.io.BufferedReader; 导入java.io.IOException; 导入java.io.InputStream; 导入java.io.InputStreamReader; 导入java.io.UnsupportedEncodingException; 导入java.util.List; 导
导入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;
公共类JSONParser{
输入流为;
字符串json;
JSONObject对象;
公共JSONObject getObject(字符串url、字符串方法、列表值)引发IOException{
如果(方法==“POST”){
DefaultHttpClient=新的DefaultHttpClient();
HttpPost=新的HttpPost(url);
试一试{
post.setEntity(新的UrlEncodedFormEntity(值));
HttpResponse response=client.execute(post);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}else if(方法==“GET”){
DefaultHttpClient=新的DefaultHttpClient();
String param=URLEncodedUtils.format(值为“utf-8”);
url+=“?”+参数;
HttpGet=新的HttpGet(url);
HttpResponse响应;
试一试{
response=client.execute(get);
is=response.getEntity().getContent();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
试一试{
BufferedReader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”);
StringBuilder sb=新的StringBuilder();
弦线;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
object=null;
}捕获(异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
对象=新的JSONObject(json);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回对象;
}
}
AmbilData.java
import java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.util.Log;
公共类AmbilData扩展异步任务{
私有JSONParser JSONParser;
ArrayList d;
JsonObjectResult作业;
语境;
进展性帕金森病;
public void init(上下文c、JsonObjectResult jres、字符串kategori、,
字符串(url){
this.context=c;
this.jobres=jres;
AmbilData ad=此;
ad.execute(url,kategori,“”);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
pd=ProgressDialog.show(上下文,“检索数据”,“aaa”);
设置消息(“请稍候…”);
pd.show();
}
@凌驾
受保护对象doInBackground(对象…参数){
//TODO自动生成的方法存根
JSONObject jsobj=null;
字符串url=(字符串)参数[0];
字符串kat=(字符串)参数[1];
Log.i(“url”,url);
Log.i(“kat”,kat);
jsonparser=新的jsonparser();
List datajson=new ArrayList();
add(新的BasicNameValuePair(“kategori”,kat));
试一试{
jsobj=jsonparser.getObject(url,“POST”,datajson);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回jsobj;
}
@凌驾
受保护的void onPostExecute(对象结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
if(pd.isShowing()){
pd.解散();
}
如果(结果!=null){
JSONObject js=(JSONObject)结果;
jobres.gotJsonObject(js);
}
}
公共静态抽象类JsonObjectResult{
公开摘要void gotJsonObject(JSONObject jobject);
}
}
请帮助我…您正在尝试将XML解析为JSON。您是在响应JSON数组还是在响应XML?那么我该怎么办?你有基于我的代码的解决方案吗@Panini我已经更新了我的帖子,请帮助@ShinkCan您可以在postexecute()方法中打印结果对象并显示给我们吗?
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;
public class JSONParser {
InputStream is;
String json;
JSONObject object;
public JSONObject getObject(String url, String method, List<NameValuePair> value) throws IOException {
if(method == "POST"){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try {
post.setEntity(new UrlEncodedFormEntity(value));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(method == "GET"){
DefaultHttpClient client = new DefaultHttpClient();
String param = URLEncodedUtils.format(value, "utf-8");
url += "?" + param;
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
is = response.getEntity().getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
json = sb.toString();
object = null;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
object = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class AmbilData extends AsyncTask<Object, Object, Object> {
private JSONParser jsonparser;
ArrayList<String> d;
JsonObjectResult jobres;
Context context;
ProgressDialog pd;
public void init(Context c, JsonObjectResult jres, String kategori,
String url) {
this.context = c;
this.jobres = jres;
AmbilData ad = this;
ad.execute(url, kategori, "");
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(context, "Retrieve Data", "aaa");
pd.setMessage("Please wait...");
pd.show();
}
@Override
protected Object doInBackground(Object... parameter) {
// TODO Auto-generated method stub
JSONObject jsobj = null;
String url = (String) parameter[0];
String kat = (String) parameter[1];
Log.i("url", url);
Log.i("kat", kat);
jsonparser = new JSONParser();
List<NameValuePair> datajson = new ArrayList<NameValuePair>();
datajson.add(new BasicNameValuePair("kategori", kat));
try {
jsobj = jsonparser.getObject(url, "POST", datajson);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsobj;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
}
if (result != null) {
JSONObject js = (JSONObject) result;
jobres.gotJsonObject(js);
}
}
public static abstract class JsonObjectResult {
public abstract void gotJsonObject(JSONObject jobject);
}
}