android取得json数据
步骤一:页面返回JSON数据。
对本空间日志“php和AJAX的简单实现”的json.php代码做一些简单修改,代码如下:将代码 echo $_GET['jsoncallback'].'('.custom_json::encode($big_test).')';
改为 echo $_GET['jsoncallback'].custom_json::encode($big_test);
将文件另存为jsondata.php。
步骤二:编写Android代码。
1.编写Contact类,设置set和get方法。
package com.domain;
public class Contact {
private String name;
private int age;
private int sex;
private Double height;
private boolean is_human;
private String string;
public Contact() { }
public Contact(String name, int age, int sex, Double height,
boolean is_human, String string) {
this.name = name;
this.age = age;
this.sex = sex;
this.height = height;
this.is_human = is_human;
this.string = string;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public boolean isIs_human() {
return is_human;
}
public void setIs_human(boolean is_human) {
this.is_human = is_human;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
2.编写输入流处理工具类StreamTools。
package com.shao.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class StreamTools {
public static byte[] readInputStream(InputStream instream) throws Exception{
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0 ;
while(( len = instream.read(buf)) != -1)
{
outstream.write(buf, 0, len);
}
byte[] data = outstream.toByteArray();//网页的二进制数据
outstream.close();
instream.close();
return data;
}
}
3.编写android布局文件main.xml。
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
新建布局文件item.xml。
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
在string.xml添加如下一行:
4.编写业务实现类。
package com.shao;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.domain.Contact;
import com.shao.utils.StreamTools;
public class MyService {
public static List
{
String path = "http://192.168.1.100:8080/WebContent/testjson/jsondata.php";
List
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream instream = conn.getInputStream();
byte[] data = StreamTools.readInputStream(instream);
String json = new String(data);
JSONArray array = new JSONArray(json);
for(int i=0;i
JSONObject item = array.getJSONObject(i);
Contact contact = new Contact(item.getString("name"),item.getInt("age"),item.getInt("sex"),item.getDouble("height"),item.getBoolean("is_human"),item.getString("string"));
contacts.add(contact);
}
return contacts;
}
}
5.编写主Activity类。
package com.shao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.domain.Contact;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class TestXML extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
List
List
ListView listview = (ListView)findViewById(R.id.listview);
for(Contact contact : contacts)
{
HashMap
item.put("name", contact.getName());
item.put("sex",contact.getSex());
item.put("age", contact.getAge());
item.put("height", contact.getHeight());
item.put("is_human", contact.isIs_human());
item.put("string", contact.getString());
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"name","sex","age","height","is_human","string"},
new int[]{R.id.name, R.id.sex, R.id.age, R.id.height, R.id.is_human, R.id.string});
listview.setAdapter(adapter);
} catch (Throwable e) {
Log.e("shao",e.toString());
Toast.makeText(this, R.string.error, 1).show();
}
}
}
6.开启访问网络功能 。
步骤三 测试。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

OnLeaks 现在与 Android Headlines 合作,首次展示了 Galaxy S25 Ultra,几天前,他试图从他的 X(以前的 Twitter)粉丝那里筹集到 4,000 美元以上的资金,但失败了。对于上下文,嵌入在 h 下面的渲染图像

除了发布两款新智能手机外,TCL 还发布了一款名为 NXTPAPER 14 的新 Android 平板电脑,其大屏幕尺寸是其卖点之一。 NXTPAPER 14 采用 TCL 标志性品牌哑光液晶面板 3.0 版本

三星尚未就何时更新其 Fan Edition (FE) 智能手机系列提供任何提示。目前来看,Galaxy S23 FE 仍然是该公司的最新版本,于 2023 年 10 月年初推出。

Vivo Y300 Pro刚刚全面亮相,它是最薄的中端Android手机之一,配备大电池。准确来说,这款智能手机的厚度仅为 7.69 毫米,但配备了 6,500 mAh 的电池。这与最近推出的容量相同

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

Redmi Note 14 Pro Plus 现已正式成为去年 Redmi Note 13 Pro Plus 的直接后继产品(亚马逊售价 375 美元)。正如预期的那样,Redmi Note 14 Pro Plus与Redmi Note 14和Redmi Note 14 Pro一起成为Redmi Note 14系列的主角。李

OnePlus的姐妹品牌iQOO的2023-4年产品周期可能即将结束;尽管如此,该品牌已宣布 Z9 系列的开发尚未结束。它的最终版,也可能是最高端的 Turbo+ 变体刚刚按照预测发布。时间
