将数据从php解析到android列表视图时出错

问题描述:

我有我的php web服务器,它从我的论坛中检索主题列表,我使用JSON来削减我的Android应用中的listview。将数据从php解析到android列表视图时出错

不幸的是运行应用程序时,当我去的网页,我应该看到名单,我得到黑画面,在我的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; 

     } 


} 

我有两个公共JSON对象是我为用户reg创建的第一个对象。一个登录名,第二个是主题列表view..where我承担错误来自

这是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:[email protected]" /> 
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:[email protected]">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"; 

我已经发现了这个和在线教程,但我对其进行编辑,以满足我的要求

我希望你有耐心我为我没有那么好,我在这个东西,想是新学

+0

你可以显示你正试图解析的字符串是否导致错误? – jiiri 2013-03-17 15:29:52

+0

打印回复日志。当我们看到“坏”json字符串时,它会更容易找到问题原因 – 2013-03-17 15:31:27

+0

我会说PHP响应以“ 2013-03-17 15:32:50

服务器返回HTTP 404错误代码,这意味着你的脚本根本不运行

验证您是否在您的应用中访问了正确的网址。

+0

OPS我发现我指挥它不完整的地址..但我现在纠正它,然后运行它后,我已编辑的更正当前php代码给我这个错误的问题03-17 16:32:05.169:E/JSON(564):解析错误:语法错误,意外的''。$ id。''(T_CONSTANT_ENCAPSED_STRING)在C:\ XAMPP \ htdocs中\ android_login_api \包括\ list_of_topics。php on line
03-17 16:32:05.289:E/JSON解析器(564):解析数据时出错org.json.JSONException:java.lang.String类型的值不能转换为JSONObject – captinmemo 2013-03-17 16:43:33