android gets json data
Step 1: The page returns JSON data.
Make some simple modifications to the json.php code of this space blog "Simple Implementation of PHP and AJAX". The code is as follows: echo the code $_GET['jsoncallback'].'('.custom_json::encode($big_test). ')';
Change to echo $_GET['jsoncallback'].custom_json::encode($big_test);
Save the file as jsondata.php.
Step 2: Write Android code.
1. Write the Contact class and set the set and get methods.
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. Write the input stream processing tool class 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();//Binary data of the web page
outstream.close();
instream.close();
return data;
}
}
3. Write the android layout file main.xml.
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
& lt; TextView
android: id = "@" +ID/name "
Android: layout_width =" 100dip "
android: layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
LinearLayout>
Create a new layout file item.xml.
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
& lt; TextView
android: id = "@" +ID/name "
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.开启访问网络功能 。
步骤三 测试。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T
