- 안드로이드에서 해당 페이지로부터 데이터를 가져오는 작업.
3-1. build.gradle에 Volley 추가
implementation 'com.android.volley:volley:1.1.1'
3-2. 게시판 리스트를 출력할 Layout 작성 - activity_board.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"
android:background="#FAFAFA">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:background="#FFF"
android:textSize="20dp"
android:textColor="#666"
android:text="게시글 목록"/>
<ImageView
android:id="@+id/img_close"
android:layout_width="28dp"
android:layout_height="28dp"
android:src="@drawable/btn_close"
android:layout_marginTop="11dp"
android:layout_marginEnd="18dp"
android:layout_alignParentEnd="true"/>
<View
android:id="@+id/viewline_01"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/title"
android:background="#EEE" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@id/viewline_01">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:divider="#EEE"
android:background="@drawable/custom_bg_01"/>
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:alpha="0.3"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
※ 화면은 다음과 같다.
4. Volley 객체를 생성한다. (싱글톤 패턴 적용)
public class MyVolley {
private static MyVolley instance;
private RequestQueue requestQueue;
public static MyVolley getInstance(Context context) {
if(instance == null)
instance = new MyVolley(context);
return instance;
}
private MyVolley(Context context) {
requestQueue = Volley.newRequestQueue(context);
}
public RequestQueue getRequestQueue() {
return requestQueue;
}
}
4.서버사이드 VO와 동일한 Board VO를 생성해준다. (동일하므로 생략)
5. Volley 객체를 이용하여 게시판 데이터를 가져온다.
public class BoardActivity extends Activity {
private Context mContext
private ListView mListView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_board);
mListView = findViewById(R.id.listview);
progressBar = findViewById(R.id.progressBar);
ImageView btnClose = findViewById(R.id.img_close);
btnClose.setOnClickListener(new View.OnClickListener() { // X 버튼
@Override
public void onClick(View view) {
finish();
}
});
getBoardData(); // 게시판 데이터 가져오기
}
private void setProgressBar(int visibility) {
progressBar.setVisibility(visibility);
}
private void getBoardData() {
setProgressBar(View.VISIBLE);
final String url = getString(R.string.url_get_board); // 서버사이드 페이지 웹 주소
RequestQueue queue = MyVolley.getInstance(mContext).getRequestQueue();
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, new JSONObject(),
successListener(), errorListener());
queue.add(jsonRequest);
}
private void parsingJSONData(String data) {
List<Board> mList = new ArrayList<>();
try {
JSONArray jArray = new JSONArray(data);
for(int i = 0; i < jArray.length(); i++) {
Board board = new Board();
JSONObject jObject = jArray.getJSONObject(i);
board.setNno(Integer.parseInt(jObject.getString("nno")));
board.setTitle(jObject.getString("title"));
board.setContent(jObject.getString("content"));
board.setCreateDate(jObject.getString("createDatet"));
mList.add(board );
}
mListView.setAdapter(new BoardAdapter(mList));
} catch(JSONException e) {
e.printStackTrace();
}
}
private Response.Listener<JSONObject> successListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
setProgressBar(View.GONE);
String result = null;
try {
result = response.getString("data");
} catch(JSONException e) {
e.printStackTrace();
}
parsingJSONData(result);
}
};
}
private Response.ErrorListener errorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Util.showToast(mContext, getString(R.string.msg_network_error_01));
// 에러 메시지 작성
}
};
}
}
'안드로이드' 카테고리의 다른 글
vibrate() is deprecated (0) | 2018.08.16 |
---|---|
Volley 사용하여 게시판 리스트 가져와서 출력하기 3/3 (0) | 2018.08.01 |
Volley 사용하여 게시판 리스트 가져와서 출력하기 1/3 (0) | 2018.08.01 |
프래그먼트 사용 시 주의사항 (0) | 2018.07.24 |
외부 앱 실행, 미 설치 시 구글 플레이로 이동 (1) | 2018.07.11 |