我正在尝试借助 PHP Web 服务在 Android 中显示数据,但我认为我的代码存在一些问题,因此无法显示数据。我在这里给出两个文件的代码
网络服务代码:
<?php
$dbhandle = mysql_connect($hostname, $username, $password,$datbase_name)
or die("Unable to connect to
MySQL");
$selected = mysql_select_db("code_Lessions",$dbhandle)
or die("Could not select examples");
$final_data = array();
$query="SELECT * FROM LessionDetail";
if ($query_run = mysql_query($query))
{
$i=0;
while($query_row = mysql_fetch_assoc($query_run))
{
$username = $query_row['LessionName'];
$password = $query_row ['CategoryName'];
$id = $query_row ['LessionId'];
//echo $username .'`s password is : '. $password.'<br>';
$data = array('LessionId'=>$id , 'LessionName'=>$username ,'CategoryName'=>$password);
$final_data[$i]=$data;
$i++;
//print(json_encode($data));
//print(json_encode($password));
}
print(json_encode($final_data));
}else{
echo mysql_error();
}
?>
文件 :JSONExampleActivity.java
package com.json.php;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://code.guru99.com/android.php");
TextView textView = (TextView)findViewById(R.id.textView1);
try {
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject jObject = new JSONObject(jsonResult);
JSONArray json = new JSONArray(jObject.getString(jsonResult));
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0;i<json.length();i++){
/*map = new HashMap<String, String>();
JSONObject e = json.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "lname" + e.getString("name"));
map.put("Category", "Category: " + e.getString("Category"));
mylist.add(map);
System.out.println("The values are: " + map.values());*/
JSONObject oneObject = json.getJSONObject(i);
// Pulling items from the array
String id = oneObject.getString("id");
String name = oneObject.getString("name");
String category = oneObject.getString("category");
textView.setText(id + "-" + name + "-" + category);
}
}
catch (JSONException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
private JSONObject JSONObject(String jsonResult) {
// TODO Auto-generated method stub
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
}
我的数据库中有两条记录,我想显示该表中的全部数据,但没有显示任何内容。