我正在尝试实现简单的 android 应用程序,我试图通过 php 站点检索数据库值,但以下代码在解析 json 数据时生成错误。
我的php文件编码json如下
getdeals.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="user"; // Database name
$tbl_name="deal"; // Table name
// Connect to server and select databse.
mysql_connect("$host","$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$q=mysql_query("SELECT * FROM deal");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
表交易如下
android类如下
public class JsonConnect extends Activity {
InputStream is;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_connect);
addListenerOnbutton();
Thread t = new Thread() {
public void run() {
getDeals();
}
};
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_json_connect, menu);
return true;
}
public void addListenerOnbutton() {
final Context context = this;
Button home = (Button)findViewById(R.id.button1);
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public void getDeals() {
String result = "";
//the year data to send
//ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/dealnow/getdeals.php");
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","dealid: "+json_data.getString("deal_id")+
", hotel name: "+json_data.getString("hotel_name")+
", valid date: "+json_data.getInt("valid_date")+
", location: "+json_data.getString("location")+
", website: "+json_data.getString("website")
);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
最后logcat输出是
05-11 01:23:08.767: D/dalvikvm(435): GC_EXPLICIT freed 24K, 4% free 6582K/6855K, paused 7ms+4ms
05-11 01:29:24.164: V/TLINE(497): new: android.text.TextLine@4065aa30
05-11 01:29:24.757: D/dalvikvm(497): GC_CONCURRENT freed 115K, 4% free 6560K/6791K, paused 5ms+14ms
05-11 01:29:24.764: E/log_tag(497): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
05-11 01:29:24.844: V/TLINE(497): new: android.text.TextLine@4065d8e0
05-11 01:34:12.147: D/dalvikvm(497): GC_EXPLICIT freed 48K, 4% free 6524K/6791K, paused 7ms+3ms
我怎样才能得到表的期望值
请帮忙。