とりあえず実現したい事は、JSONをHTTPのgetで受け取ってListViewで表示までを目標に。
で、下記のように実装。
(URIベタ書きですが...、とりあえず動かしたいので。)
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class JsonListActivity extends Activity { private String strUri = "http://1.latest.slim3demo001.appspot.com/AjaxTweet.json"; private String json = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, 1000); HttpConnectionParams.setSoTimeout(params, 1000); HttpGet httpRequest = new HttpGet(strUri); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpRequest); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); try { json = EntityUtils.toString(httpEntity); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpEntity.consumeContent(); } catch (IOException e) { e.printStackTrace(); } } } httpClient.getConnectionManager().shutdown(); ArrayAdaptermain.xmlは下記のようにadapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1); try { JSONArray jsonArray = new JSONArray(json); int cnt = jsonArray.length(); List jlist = new ArrayList (); for (int i = 0; i < cnt; i++) { jlist.add(jsonArray.getJSONObject(i)); } for (JSONObject obj : jlist) { String item = obj.getString("content"); adapter.add(item); } } catch (JSONException e) { e.printStackTrace(); } ListView listView = (ListView)findViewById(R.id.jsonlistview); listView.setAdapter(adapter); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/jsonlistview" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>ネットにつなげるので、Manifestファイルにも"android.permission.INTERNET"を追加
<uses-permission android:name="android.permission.INTERNET"/>一応無事に動きました。 参考サイト: Android で JSON を使おう ~ 前編~CommentsAdd Star Android で JSON を使おう ~ 後編 ~CommentsAdd Star
0 件のコメント:
コメントを投稿