解析数据时的错误org.json.jsonexception输入结束时的字符1

时间:2012-08-23 10:44:06

标签: java php android mysql json

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

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

  

错误解析数据org.json.jsonexception输入结束时的字符1

以下是代码:

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);
}
?>

请有人帮助我吗?

3 个答案:

答案 0 :(得分:0)

变化

  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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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当用户到达列表视图的末尾时