我是 android 开发新手,我根据本教程构建了一个 android 应用程序... Android 教程 22 - JSON Array 数据在 ListView 中使用 ArrayList 和自定义类显示
我已经完成了教程所说的一切,但是当我运行模拟器时,我收到以下消息:
解析数据时出错 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);
}
?>
请问有人可以帮我吗?