【问题标题】:Error parsing data org.json.jsonexception end of input at character 1 of解析数据 org.json.jsonexception 在字符 1 处输入结束时出错
【发布时间】:2012-08-23 10:44:05
【问题描述】:

我是 android 开发新手,我根据本教程构建了一个 android 应用程序...Android Tutorial 22 - JSON Array data display in ListView using ArrayList with custom class

我已经完成了教程所说的一切,但是当我运行模拟器时,我收到以下消息:

解析数据时出错 org.json.jsonexception end of input at character 1 of

代码如下:

public class Antallaktika extends Activity {

ArrayList<proionta> arrayOfWebData = new ArrayList<proionta>();

class proionta {
    public String product_name;
    public String product_sku;
    public String product_price;
}

FancyAdapter aa=null;

static ArrayList<String> resultRow;

public void onCreate(Bundle savedInstanceState) {
    try{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.antallaktika);
        String result = "";

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://machina.gr/antallaktika2.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream webs = entity.getContent();

        try{
            BufferedReader reader = new BufferedReader (new InputStreamReader(webs, "ISO-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line = "\n");
            }
            webs.close();
            result=sb.toString();
        }
        catch (Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }
    }
        catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
        }
    try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            proionta resultRow = new proionta();
            resultRow.product_name = json_data.getString("jos_vm_product.product_name");
            resultRow.product_sku = json_data.getString("jos_vm_product.product_sku");
            resultRow.product_price = json_data.getString("jos_vm_product_price.product_price");
            arrayOfWebData.add(resultRow);
        }
    }
    catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }
    ListView myListView = (ListView)findViewById(R.id.myListView);
    aa=new FancyAdapter();
    myListView.setAdapter(aa);
    }
    catch (Exception e){
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());
        e.printStackTrace();
    }
}

class FancyAdapter extends ArrayAdapter<proionta> {
    FancyAdapter() {
        super(Antallaktika.this, android.R.layout.simple_list_item_1, arrayOfWebData);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            LayoutInflater inflater=getLayoutInflater();
            convertView=inflater.inflate(R.layout.customgrid, null);
            holder=new ViewHolder (convertView);
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arrayOfWebData.get(position));
        return(convertView);
    }
}

class ViewHolder{
    public TextView product_name=null;
    public TextView product_sku=null;
    public TextView product_price=null;

    ViewHolder(View customgrid) {
        product_name=(TextView)customgrid.findViewById(R.id.product_name);
        product_sku=(TextView)customgrid.findViewById(R.id.product_sku);
        product_price=(TextView)customgrid.findViewById(R.id.product_price);
    }
    void populateFrom(proionta r){
        product_name.setText(r.product_name);
        product_sku.setText(r.product_sku);
        product_price.setText(r.product_price);
    }
}

}

而php文件是:

<?php

$databasehost = "localhost";
$databasename = "xxxxxxxxxxx";
$databaseusername = "xxxxxxxxxxxxxx";
$databasepassword = "xxxxxxxxxxxxx";

$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());

$query = ("SELECT jos_vm_product.product_name, jos_vm_product.product_sku, jos_vm_product_price.product_price
FROM jos_vm_product, jos_vm_product_category_xref, jos_vm_product_price
WHERE jos_vm_product_category_xref.category_id=2 
   AND jos_vm_product_category_xref.product_id= jos_vm_product.product_id
   AND jos_vm_product.product_id=jos_vm_product_price.product_id
ORDER BY jos_vm_product.product_name");
$sth = mysql_query($query);

if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query. '\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r;
    }
print json_encode($rows);
}
?>

谁能帮帮我?

【问题讨论】:

    标签: java php android mysql json


    【解决方案1】:

    改变

      resultRow.product_name = json_data.getString("jos_vm_product.product_name");
            resultRow.product_sku = json_data.getString("jos_vm_product.product_sku");
            resultRow.product_price = json_data.getString("jos_vm_product_price.product_price");
    

      resultRow.product_name = json_data.getString("product_name");
            resultRow.product_sku = json_data.getString("product_sku");
            resultRow.product_price = json_data.getString("product_price");
    

    在您的 json 文件中,标签的列出方式与引用 url 一样

    【讨论】:

    • 我忘了说 php 包含希腊语和英语字符....会引起问题吗?例如:BufferedReader reader = new BufferedReader (new InputStreamReader(webs, "ISO-8859-1"),8);
    • 只需删除这些内容并在 BufferedReader reader = new BufferedReader(new InputStreamReader(is));并且还尝试使用 jst 包含英文的 o/p 请提供您的错误日志
    • 只需删除这些东西并在 BufferedReader reader = new BufferedReader(new InputStreamReader(webs));并且还尝试使用 jst 包含英文的 o/p 请提供您的错误日志
    • 我已经尝试过您的说明,但仍然出现同样的错误....我不知道这是否对您有帮助,但正如我看到的那样,它是使用 utf-8_general_ci 创建的数据库...
    • 请复制你的日志猫并粘贴到这里
    【解决方案2】:

    即使我的应用程序也遇到了这个异常,但在我的情况下,它是来自服务器的 JSONException 为 null ......我认为在你的情况下也可能是同样的问题......可能是你的 php 标签或响应来自 php 的链接不正确或即将 nul.... 似乎...

    【讨论】:

    • 您知道如何解决吗?我测试了 php 到网络浏览器,它正确地显示了我的结果。
    • 我对 php 不熟悉...所以我不知道 php...在我的情况下是服务器问题...
    【解决方案3】:
    08-23 11:57:48.639: E/dasd(336): [{"product_name":"250 1?.70,5 ??","product_sku":"??.86.???.LBR.169","product_price":"0.00000"},{"product_name":"BI?? 6?12","product_sku":"??.81.???.SEP.553","product_price":"0.00000"},{"product_name":"BI?? 8X16 TSEI","product_sku":"??.81.???.SEP.554","product_price":"0.00000"},{"product_name":"BOCCOLA","product_sku":"??.99.???.SEP.223","product_price":"0.00000"},{"product_name":"BOCCOLA - ?????????? ??????? ??? MFC150-1000","product_sku":"??.81.???.SEP.234","product_price":"1.57500"},{"product_name":"BOCOLLA","product_sku":"??.99.???.SEP.390","product_price":"0.00000"},{"product_name":"CARTUCCIA INTERMOTOR ??? IM252\/350 (226)","product_sku":"??.86.???.LBR.124","product_price":"1.84800"},{"product_name":"D1140","product_sku":"??.99.???.SEP.476","product_price":"0.00000"},{"product_name":"E??????? ???????","product_sku":"??.85.???.PAT.073","product_price":"0.00000"},{"product_name":"FOTORESISTENZA FC","product_sku":"??.86.???.ITM.011","product_price":"0.00000"},{"product_name":"GROUP BILANCERE (4330.039 VECH) (L25)","product_sku":"??.86.???.LBR.160","product_price":"14.70000"},{"product_name":"JOYSTICK RIVER 21M4000069-4C016","product_sku":"??.97.???.ORS.103","product_price":"210.00000"},{"product_name":"KIT PRONTO INTERVENTO AR19","product_sku":"??.86.???.A&R.034","product_price":"34.12500"},{"product_name":"KIT PRONTO INTERVENTO AR30","product_sku":"??.86.???.A&R.037","product_price":"34.12500"},{"product_name":"KIT PRONTO INTERVENTO AR503","product_sku":"??.86.???.A&R.039","product_price":"34.12500"},{"product_name":"KIT ??? ??????????? QP BRIGGS & STRATTON","product_sku":"??.89.???.SIG.059","product_price":"0.00000"},{"product_name":"NTIZA AYTOKINH??? UNIVERSALE","product_sku":"??.89.???.SIG.069","product_price":"13.25100"},{"product_name":"NTIZA AYTOKIN???? TBS 47","product_sku":"??.89.???.SIG.068","product_price":"16.02300"},{"product_name":"OR 53.4X49.8X1.8","product_sku":"??.81.???.SEP.430","product_price":"0.00000"},{"product_name":"OR 65?61,4?1,8","product_sku":"??.81.???.SEP.340","product_price":"1.15500"},{"product_name":"OR ?12,37X2,62 OR3050","product_sku":"??.81.???.SEP.608","product_price":"0.00000"},{"product_name":"OR ?15,08?2,62 OR119","product_sku":"??.81.???.SEP.552","product_price":"0.00000"},{"product_name":"OR ?15,60?1,78 OR2062","product_sku":"??.81.???.SEP.591","product_price":"0.00000"},{"product_name":"OR ?34,5?3,53 OR 4137","product_sku":"??.81.???.SEP.607","product_price":"0.00000"},{"product_name":"OR ?39,69?3,53 OR144","product_sku":"??.81.???.SEP.531","product_price":"0.00000"},{"product_name":"OR ?44,45?3,53 OR149","product_sku":"??.81.???.SEP.551","product_price":"0.00000"},{"product_name":"OR ?45,69?2,62 OR3181","product_sku":"??.81.???.SEP.532","product_price":"0.00000"},{"product_name":"OR ?49?2","product_sku":"??.81.???.SEP.592","product_price":"0.00000"},{"product_name":"OR ?55,25?2,62 OR3218","product_sku":"??.81.???.SEP.570","product_price":"0.00000"},{"product_name":"OR ?9,113?2,62 OR109","product_sku":"??.81.???.SEP.569","product_price":"0.00000"},{"product_name":"OR ?9,92?2,62","product_sku":"??.81.???.SEP.590","product_price":"0.00000"},{"product_name":"ORING 114","product_sku":"??.85.???.PAT.025","product_price":"0.00000"},{"product_name":"ORING 114","product_sku":"??.85.???.PAT.031","product_price":"0.00000"},{"product_name":"ORING 2010","product_sku":"??.85.???.PAT.028","product_price":"0.00000"},{"product_name":"ORING 2018","product_sku":"??.85.???.PAT.029","product_price":"0.00000"},{"product_name":"ORING 3162","product_sku":"??.85.???.PAT.033","product_price":"0.00000"},{"product_name":"ORING 3175","product_sku":"??.85.???.PAT.006","product_price":"0.00000"},{"product_name":"ORING OR 3112 ???????? ?????????  4\/5 ??????","product_sku":"??.93.???.ABA.032","product_price":"0.31500"},{"product_name":"P.T.O ??? SEP 1000","product_sku":"??.81.???.SEP.089","product_price":"0.00000"},{"product_name":"PAGLIETTA INTERMOTOR ??? IM252\/350 (146)","product_sku":"??.86.???.LBR.134","product_price":"1.38600"},{"product_name":"PIATTELLO INTERMOTOR ??? IM252\/
    

    看看它不会以 ']' 结尾,太多数据无法保存在字符串中,一次只发送 10 个 prdct,当用户到达列表视图的末尾时再发送 10 个 prdct

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-27
      • 2014-06-03
      • 2013-08-20
      • 2014-07-11
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多