ホームページ  >  記事  >  バックエンド開発  >  アンドロイドはJSONデータを取得します

アンドロイドはJSONデータを取得します

巴扎黑
巴扎黑オリジナル
2016-11-24 11:06:10963ブラウズ

ステップ 1: ページは JSON データを返します。

このスペース ブログ「PHP と AJAX の単純な実装」の json.php コードにいくつかの簡単な変更を加えます。コードは次のとおりです。コード $_GET['jsoncallback'].'('.custom_json::encode) をエコーし​​ます。 ($big_test). ')';

echo $_GET['jsoncallback'].custom_json::encode($big_test); に変更します

ファイルを jsondata.php として保存します。

ステップ 2: Android コードを作成します。

1. Contact クラスを作成し、set メソッドと get メソッドを設定します。

package com.domain;

public クラス Contact {
private int age;
private boolean is_human;

public }

public Contact(String name, int age, int sex, Double height,
boolean is_human, String string) {
this.name = 名前;
this.sex = 性別;
this.is_human = is_human;
this.string = 文字列;
public String getName() {
return name;

public void setName(String name);
}

public int getAge() {
年齢を返す
}

public void setAge(int age) {
this.age = 年齢
}

public int getSex() {
性別を返す

public void setSex(int sex) {
this.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 void setString(String string) {
this.string = string;
}

}

2. 入力ストリーム処理ツール クラス StreamTools を記述します。 Package com.shao.utils。 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();//Web ページのバイナリデータ
instream.close();

}

}

3. Android レイアウト ファイル main.xml を書き込みます。


android:orientation = "垂直"
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:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> ;

新しいレイアウト ファイル item.xml を作成します。


android:orientation = "垂直"
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:id="@+id/sex"
android:layout_width="100dip"
    android:layout_height="wrap_content"
/> 

android:id="@+id/age"
android:layout_width="100dip"
android:layout_height="wrap_content"
/> 
android:id="@+id/height"
android:layout_width="100dip"
android:layout_height="wrap_content"
/> 
android:id="@+id/is_human"
android:layout_width="100dip"
android:layout_height="wrap_content"
/> 
android:id="@+id/string"
android:layout_width="100dip"
android:layout_height="wrap_content"
/> 

 

在string.xml追加如下一行:

net error 

4. パッケージ com.shao; 

java.io.InputStreamをインポートします。 
java.net.HttpURLConnectionをインポートします。 
java.net.URLをインポートします。 
java.util.ArrayList をインポートします。 
java.util.Listをインポートします。 

org.json.JSONArray をインポートします。 
org.json.JSONObject をインポートします。 

com.domain.Contact をインポートします。 
com.shao.utils.StreamToolsをインポートします。 

public class MyService {

public static List getLastContact() throws Throwable
{
String path = "http://192.168.1.100:8080/WebContent/testjson/jsondata.php"; 
リスト<連絡先> contacts = new ArrayList(); 
URL url = 新しい URL(パス); 
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 配列 = new JSONArray(json); 
for(int i=0;i {
JSONObject item = array.getJSONObject(i); 
連絡先 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); 
}

連絡先を返します。 
}


}

5.编写主Activity类。

package com.shao; 

import java.util.ArrayList; 
java.util.HashMapをインポートします。 
java.util.Listをインポートします。 

com.domain.Contact をインポートします。 

android.app.Activityをインポートします。 
android.os.Bundleをインポートします。 
android.util.Logをインポートします。 
android.widget.ListViewをインポートします。 
android.widget.SimpleAdapter をインポートします。 
android.widget.Toastをインポートします。 

public class TestXML extends Activity {
/**アクティビティが最初に作成されるときに呼び出されます。*/
@Override
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
{
リスト<連絡先> を試してください。連絡先 = MyService.getLastContact(); 
リスト<ハッシュマップ<文字列,オブジェクト>> data = new ArrayList>(); 

ListView listview = (ListView)findViewById(R.id.listview); 
for(連絡先 contact : contacts)
{
HashMap item = 新しい HashMap(); 
item.put("名前", contact.getName()); 
item.put("セックス",contact.getSex()); 
item.put("年齢", contact.getAge()); 
item.put("高さ", contact.getHeight()); 
item.put("is_human", contact.isIs_human()); 
item.put("string", contact.getString()); 
    data.add(項目); 

}





SimpleAdapter アダプター = new SimpleAdapter(this, data, R.layout.item,
new String[]{"名前","性別","年齢","身長","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(アダプター); 



} catch (Throwable e) {
Log.e("shao",e.toString()); 
Toast.makeText(this, R.string.error, 1).show(); 
}
}
}



6.开启访问ネットワーク機能。 

步ステップ三試験。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。