Php 如何在Android中将Json字符串转换为ListView
希望有人能帮助我理解这一点,我完全迷路了,似乎根本无法理解这一点 我有一个从数据库中提取信息的应用程序,目前,Android连接到一个PHP页面,结果打印在应用程序上,如:Php 如何在Android中将Json字符串转换为ListView,php,android,sql,database,json,Php,Android,Sql,Database,Json,希望有人能帮助我理解这一点,我完全迷路了,似乎根本无法理解这一点 我有一个从数据库中提取信息的应用程序,目前,Android连接到一个PHP页面,结果打印在应用程序上,如: {“优惠”:“半价饮料”、“一天”:“星期一”} {“优惠”:“半价饮料”,“一天”:“星期二”} 我需要将其转换为列表视图,是否有人可以建议我如何执行此操作 我已经在连接到php页面的页面中输入了这些信息 public class BarsActivity extends Activity { TextView
{“优惠”:“半价饮料”、“一天”:“星期一”}
{“优惠”:“半价饮料”,“一天”:“星期二”}
我需要将其转换为列表视图,是否有人可以建议我如何执行此操作
我已经在连接到php页面的页面中输入了这些信息
public class BarsActivity extends Activity {
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://10.0.2.2/SocialCrawler/content.php"; //i use my real ip here
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("day","Monday"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
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","day: "+json_data.getString("day")+
", offer: "+json_data.getString("offer")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
公共类BarsActivity扩展活动{
文本视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个粗略的视图-这应该通过布局资源进行设置
//但是,因为这是一个示例,所以不需要在XML中声明它们。
LinearLayout rootLayout=新的LinearLayout(getApplicationContext());
txt=新文本视图(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
//设置文本并调用connect函数。
txt.setText(“连接…”);
//调用该方法以运行数据检索
setText(getServerData(KEY_121));
}
公共静态最终字符串键_121=”http://10.0.2.2/SocialCrawler/content.php“;//我在这里使用我的真实ip
私有字符串getServerData(字符串返回字符串){
InputStream=null;
字符串结果=”;
//要发送的年度数据
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“日”、“星期一”);
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(键号121);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i通过迭代所有键->值将json转换为ArrayList,并诚实地使用ArrayAdapter。互联网上有很多这样的例子。在这个网站上有很多这样的例子。数据如何映射到ListView?一个视图会显示今天的报价,还是ListView会显示今天的报价和即将到来的报价?i我只会显示当天的报价列表,我有它,所以用户选择了一天,然后将这一天传递到PHP文件,该文件返回当天的报价。也许有人可以帮助我完成教程,我已经获取了源代码并尝试输入我的信息,我说的唯一需要注意的事情是正确的吗nged是Main.java上文件的url、id、eqid和magnity,它们必须转换为数据库中的变量?