我试图在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;
}
}
我的数据库中有两条记录,我想显示该表中的整个数据,但没有显示任何内容。
答案 0 :(得分:0)
这不是一个完整的答案,但您可以使用以下内容来帮助调试代码。
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
Log.d("JSONDebug" , jsonResult);
这将显示您在日志记录中收到的输入。所以从那里可以更容易地确定json是否处于wring格式或者读者的输入是否完全没有,我认为更可能是这种情况,因为从你的帖子看起来你似乎没有得到jsonParser错误