Php 如何获取android应用程序的值
我正在开发一个基于web服务的android应用程序。如何基于单击的项目运行查询(使用php和MySQL的服务器)并从web获取结果。我还想将一些值显示为列表(列表布局)。有没有办法做到这一点?你想做的是开发一个rest api,为你的android应用程序提供数据。例如,你的网站有一些你想在你的应用程序中使用的内容,然后你可以编写一个php脚本,只返回特定格式的数据 例如,mysite.net/rest/fetchAllLocations.php?可能是一些参数 这将以json格式返回位置,下面是一个示例: [{“id”:1,“shop_lng”:8.5317153930664,“shop_lat”:52.02480316121,“shop_zipcode”:33602,“shop_city”:“Bielefeld”,“shop_street”:“Arndtstra\u00dfe”,“shop_snumber”:3,“shop_name”:“M\u00fcller”,“shop_desc”:“Kaufhaus”}] 以下是rest api请求的示例: 因此,当您设置了RESTAPI后,您就可以用android手机接收数据了。我使用静态方法获取此数据:Php 如何获取android应用程序的值,php,android,mysql,Php,Android,Mysql,我正在开发一个基于web服务的android应用程序。如何基于单击的项目运行查询(使用php和MySQL的服务器)并从web获取结果。我还想将一些值显示为列表(列表布局)。有没有办法做到这一点?你想做的是开发一个rest api,为你的android应用程序提供数据。例如,你的网站有一些你想在你的应用程序中使用的内容,然后你可以编写一个php脚本,只返回特定格式的数据 例如,mysite.net/rest/fetchAllLocations.php?可能是一些参数 这将以json格式返回位置,下
public class JsonGrabber{
public static JSONArray receiveData(){
String url = "your url";
String result = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(url);
HttpResponse res = null;
try {
res = client.execute(method);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try{
InputStream is = res.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
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());
}
JSONArray jArray = null;
try{
jArray = new JSONArray(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
好了,就是这样,一旦你有了json格式的数据,你只需要解析它:
JSONArray测试=(JSONArray)JsonGrabber.receiveData()
试试{
对于(int i=0;iThanks ArtWorkAD。让我试试这个。你知道如何在android中使用SAX解析XML吗。我尝试了一些链接,但找不到更好的解决方案。我想从XML文件中获取一些类别名称,并将其列在android手机中。你可以在另一个问题中问这个问题
try {
for(int i=0;i<test.length();i++){
JSONObject json_data = test.getJSONObject(i);
int id = json_data.getInt("id");
}
}