PHP Web サービスを使用して Android でデータを表示しようとしていますが、コードに問題があると思われ、データが表示されません。ここに両方のファイルのコードを示します
Web サービスのコード:
<?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;
}
}
データベースに 2 つのレコードがあり、そのテーブルのデータ全体を表示したいのですが、何も表示されません。