textview에다가 요청결과를 보여줬는데
좀더 보기 편하게 listview에다 넣는 코드입니다.
WebRequestListView.java
public class WebRequestListView extends FragmentActivity { private FragmentManager fm; private LoadingDialog loadingDlg; private MyItemAdapter adapter; private ListView lv; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.web_request_list_view); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); fm = ((FragmentActivity)this).getSupportFragmentManager(); lv = (ListView)findViewById(R.id.list); loadingDlg = new LoadingDialog(); loadingDlg.setMsg("loading..."); loadingDlg.setCancelable(false); loadingDlg.setNoBtn(true); loadingDlg.show(fm, null); retrieveContent(); } private void retrieveContent() { new AsyncTask<Void, Void, List<MyItem>>() { @Override protected List<MyItem> doInBackground(Void... params) { return getWebPage("http://1-dot-beatgameranking.appspot.com/read"); } @Override protected void onPostExecute(List<MyItem> items) { loadingDlg.dismiss(); adapter = new MyItemAdapter(WebRequestListView.this, R.layout.list_view_item_rank, items); adapter.setContext(WebRequestListView.this); lv.setAdapter(adapter); } }.execute(null, null, null); } public class MyItemAdapter extends ArrayAdapter<MyItem> { private List<MyItem> items; private Context context; private int layoutResource; public void setContext(Context c) { this.context = c; } public MyItemAdapter(Context context, int layoutResource, List<MyItem> items) { super(context, layoutResource, items); this.context = context; this.items = items; this.layoutResource = layoutResource; } @Override public View getView(int position, View convertView, ViewGroup parent) { final MyItem myItem = items.get(position); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(layoutResource, null); } if (myItem != null) { TextView content = (TextView) convertView.findViewById(R.id.content); if (content != null){ content.setTextSize(20); content.setTextColor(Color.BLACK); content.setText(myItem.getContent()); } } return convertView; } } public class MyItem { public String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } public List<MyItem> getWebPage(String webpage) { List<MyItem> items = new ArrayList<MyItem>(); HttpURLConnection urlConnection = null; try { URL url = new URL(webpage); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Accept-Charset", "UTF-8"); urlConnection.setRequestProperty("charset", "UTF-8"); urlConnection.setConnectTimeout(6000); urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8")); writer.close(); wr.close(); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { MyItem item = new MyItem(); String content = inputLine; item.setContent(content); items.add(item); } in.close(); } } catch (Exception e) { String msg = e.toString(); } finally { urlConnection.disconnect(); } return items; } }
web_request_list_view.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/button1"/> </RelativeLayout>
댓글 없음:
댓글 쓰기