Heim >Backend-Entwicklung >PHP-Tutorial >Android erhält JSON-Daten
Schritt 1: Die Seite gibt JSON-Daten zurück.
Nehmen Sie einige einfache Änderungen am json.php-Code dieses Space-Blogs „Einfache Implementierung von PHP und AJAX“ vor. Der Code lautet wie folgt: echo den Code echo $_GET['jsoncallback'].'(' .custom_json::encode( $big_test).')';
Ändern Sie die Datei in echo $_GET['jsoncallback'].custom_json::encode($big_test); jsondata.php.
Schritt 2: Android-Code schreiben.
1. Schreiben Sie die Contact-Klasse und legen Sie die Set- und Get-Methoden fest.
Paket com.domain;
private String name;
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.sex = sex;
this.is_human = is_human ;
this.string = string;
}
public String getName() {
return name; this.name = name;
}
public int getAge() {
return age; .age = age;
}
public int getSex() {
return sex; this. sex = sex;
public Double getHeight() {
return height; .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. Schreiben Sie die Eingabestream-Verarbeitungstoolklasse StreamTools.
package com.shao.utils;
import java.io.ByteArrayOutputStream;
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); () ;//Binärdaten der Webseite
outstream.close();
instream.close();
Rückgabedaten
}
3. Schreiben Sie die Android-Layoutdatei 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"
/>
Neues Layoutdateielement .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;
java.net.URL importieren;
import java.util.ArrayList;
java.util.List importieren;
import org.json.JSONArray;
import org.json.JSONObject;
com.domain.Contact importieren;
import com.shao.utils.StreamTools;
public class MyService {
public static List
{
String path = "http://192.168.1.100:8080/WebContent/testjson/jsondata.php";
Liste
URL url = neue URL(Pfad);
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);
Kontakt 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);
}
Kontakte zurückgeben;
}
}
5.编写主Aktivität类。
Paket com.shao;
import java.util.ArrayList;
import java.util.HashMap;
java.util.List importieren;
com.domain.Contact importieren;
android.app.Activity importieren;
android.os.Bundle importieren;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; setContentView(R.layout.main);
versuchen Sie es mit {
List
List
ListView listview = (ListView)findViewById(R.id.listview);
for(Kontaktkontakt: Kontakte)
{
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.开启访问网络功能 。
步骤三 测试。