Java 如何使用ListActivity在android中列出数据库中的帖子?
我试图从数据库中检索数据,并在android中列出它们,但我不知道如何做到这一点,所以我使用了listavtivity和其他一些php代码,但它不能作为应用程序使用。我只是想问,这是正确的方法吗 php代码:Java 如何使用ListActivity在android中列出数据库中的帖子?,java,php,android,mysql,Java,Php,Android,Mysql,我试图从数据库中检索数据,并在android中列出它们,但我不知道如何做到这一点,所以我使用了listavtivity和其他一些php代码,但它不能作为应用程序使用。我只是想问,这是正确的方法吗 php代码: <?php //turn off error reporting error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); //Create fields for the database //server, us
<?php
//turn off error reporting
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
//Create fields for the database
//server, username, password, database
$dbhost = "mysql8.000webhost.com";
$dbuser = "a3399702_hassoba";
$dbpass = "password";
$dbdb = "a3399702_sa3dni";
//connect to mySQL
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
//Select the database
mysql_select_db($dbdb, $connect) or die("database selection error");
//Query the table android login
$query = mysql_query("SELECT * FROM signup_db");
//Create a while loop that places the returned data into an array
while ($list = mysql_fetch_assoc($query))
//Store the returned data into a variable
$output[] = $list;
//encode the returned data in JSON format
Print(json_encode($output));
//close the connection
mysql_close();
?>
java代码:
package com.listposts;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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;
import android.app.ListActivity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class SeeRequests extends ListActivity {
InputStream is = null;
StringBuilder sb = null;
String result = null;
String classes[];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getData();
setListAdapter(new ArrayAdapter<String>(SeeRequests.this,
android.R.layout.simple_list_item_1, classes));
}
private void getData() {
// TODO Auto-generated method stub
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://sa3dnishokran.netne.net/getrequests.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error: " + e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
sb = new StringBuilder();
String line = "0";
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());
}
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
classes[i] = json_data.getString("firstname");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "Wrong ID or Password",
Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try {
Class ourClass = Class.forName("com.sa3dnishokran." + cheese);
Intent ourIntent = new Intent(AddProfileActivity.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package.com.listposts;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.net.ParseException;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类SeeRequests扩展了ListActivity{
InputStream=null;
StringBuilder sb=null;
字符串结果=null;
字符串类[];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
getData();
setListAdapter(新阵列适配器)(请参见requests.this、,
android.R.layout.simple_list_item_1,class));
}
私有void getData(){
//TODO自动生成的方法存根
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://sa3dnishokran.netne.net/getrequests.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
Log.e(“Log_标记”,“错误:+e.toString());
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
sb=新的StringBuilder();
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
试一试{
JSONArray jArray=新JSONArray(结果);
JSONObject json_data=null;
for(int i=0;i
要在列表视图中显示数据
您需要使用列表适配器
。这里有一个可能对您有所帮助的教程:这种方法对我来说似乎很好。另一种方法是实现soapweb服务而不是JSON,但是,在这种情况下,您需要手动解析SOAP响应,或者使用其他工具/库来解析SOAP响应。这些工具目前对于Android来说还不够成熟。我个人更喜欢JSON方式,应用程序崩溃的原因似乎是Android框架特有的其他问题,与您从数据库获取数据的方法无关。定义“它不起作用”。一个建议是,不要在UI线程中执行与网络相关的操作,而是使用asyncTask
。