我有我的php web服务,从我的论坛中检索主题列表,我使用JSON来削减我的Android应用程序中的listview。
不幸的是,在运行应用程序时,当我进入我应该看到该列表的页面时,我得到黑色的空白屏幕并且在我的logcat中出现了此错误
03-17 15:00:04.189: E/JSON Parser(379): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
这是我的JSONparser类
package com.example.androidhive.library;
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() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// 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();
} 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();
Log.e("JSON", json);
} 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;
}
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;
}
}
我有两个公共JSONobject,第一个用于用户注册。登录,第二个是主题列表视图..我假设错误来自
这是php
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . 'DB_Connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
if(isset($_GET['parent']))
{
$id = intval($_GET['parent']);}
$result = mysql_query("select t.id, t.title, u.username as author, from topics as t left join user as u where t.parent="'.$id.'" and t.id2=1 group by t.id order by t.timestamp2 desc');
") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["topics"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$topic = array();
$topic["tid"] = $row["t.id"];
$topic["name"] = $row["t.title"];
$topic["author"] = $row["u.username"];
// push single product into final response array
array_push($response["topics"], $topic);
echo json_encode($response);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No topics found";
// echo no users JSON
echo json_encode($response);
}
?>
这是我从服务器获取的字符串
03-17 16:04:21.599: E/JSON(502): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
03-17 16:04:21.599: E/JSON(502): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
03-17 16:04:21.599: E/JSON(502): <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
03-17 16:04:21.599: E/JSON(502): <head>
03-17 16:04:21.599: E/JSON(502): <title>Object not found!</title>
03-17 16:04:21.599: E/JSON(502): <link rev="made" href="mailto:postmaster@localhost" />
03-17 16:04:21.599: E/JSON(502): <style type="text/css"><!--/*--><![CDATA[/*><!--*/
03-17 16:04:21.599: E/JSON(502): body { color: #000000; background-color: #FFFFFF; }
03-17 16:04:21.599: E/JSON(502): a:link { color: #0000CC; }
03-17 16:04:21.599: E/JSON(502): p, address {margin-left: 3em;}
03-17 16:04:21.599: E/JSON(502): span {font-size: smaller;}
03-17 16:04:21.599: E/JSON(502): /*]]>*/--></style>
03-17 16:04:21.599: E/JSON(502): </head>
03-17 16:04:21.599: E/JSON(502): <body>
03-17 16:04:21.599: E/JSON(502): <h1>Object not found!</h1>
03-17 16:04:21.599: E/JSON(502): <p>
03-17 16:04:21.599: E/JSON(502): The requested URL was not found on this server.
03-17 16:04:21.599: E/JSON(502):
03-17 16:04:21.599: E/JSON(502): If you entered the URL manually please check your
03-17 16:04:21.599: E/JSON(502): spelling and try again.
03-17 16:04:21.599: E/JSON(502):
03-17 16:04:21.599: E/JSON(502): </p>
03-17 16:04:21.599: E/JSON(502): <p>
03-17 16:04:21.599: E/JSON(502): If you think this is a server error, please contact
03-17 16:04:21.599: E/JSON(502): the <a href="mailto:postmaster@localhost">webmaster</a>.
03-17 16:04:21.599: E/JSON(502): </p>
03-17 16:04:21.599: E/JSON(502): <h2>Error 404</h2>
03-17 16:04:21.599: E/JSON(502): <address>
03-17 16:04:21.599: E/JSON(502): <a href="/">10.0.2.2</a><br />
03-17 16:04:21.599: E/JSON(502): <span>Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7</span>
03-17 16:04:21.599: E/JSON(502): </address>
03-17 16:04:21.599: E/JSON(502): </body>
03-17 16:04:21.599: E/JSON(502): </html>
03-17 16:04:21.599: E/JSON Parser(502): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
url并从服务器获取数据,参数为
private static String url_all_topics = "http://10.0.2.2/android_login_api/list_of_topics.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_TOPICS = "topics";
private static final String TAG_TOPICSID = "t.id";
private static final String TAG_TOPICTITLE = "t.title";
private static final String TAG_TOPICAUTHOR = "u.username";
我在和在线教程中找到了这个,但我编辑它以满足我的要求
我希望你对我有耐心,因为我不是很好,而且我是新手,想要学习