0

Android 開発は初めてで、このチュートリアルに従って Android アプリケーションを作成しました... Android チュートリアル 22 - カスタム クラスで ArrayList を使用して ListView に JSON 配列データを表示

チュートリアルに記載されていることはすべて実行しましたが、エミュレーターを実行すると、次のメッセージが表示されます。

データの解析エラー 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);
}
?>

誰でも私を助けることができますか?

4

3 に答える 3

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を参照したようにリストされています

于 2012-08-23T10:48:40.870 に答える
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 を送信します

于 2012-08-23T12:02:09.400 に答える
0

私もアプリケーションでこの例外を受け取りましたが、私の場合はサーバーからのJSONExceptionがnullでした...あなたの場合も同じ問題があると思います...あなたのphpタグまたはphpリンクからの応答である可能性があります正しくないか、ヌルになる....そう思われる...

于 2012-08-23T10:58:46.770 に答える