본문으로 바로가기
반응형

HTTP, Async, Gson, 이미지 라이브러리


/**
 * HTTPClient Library
 * https://github.com/smarek/httpclient-android
 */
implementation 'cz.msebera.android:httpclient:4.5.8'
 
/**
 * Loopj Http Library
 * https://github.com/loopj/android-async-http
 */
implementation 'com.loopj.android:android-async-http:1.4.9'
 
/**
 * Gson
 * https://github.com/google/gson
 */
implementation 'com.google.code.gson:gson:2.8.2'
 
/**
 * 이미지 라이브러리
 * https://github.com/bumptech/glide
 */
implementation 'com.github.bumptech.glide:glide:4.3.0'



Manifests 추가 설정

<!-- 네트워크 기능 사용 권한 -->
<uses-permission android:name="android.permission.INTERNET"/>
 
<activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">




통신 결과 처리 단일 추상화 클래스


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.Toast;
 
import com.loopj.android.http.TextHttpResponseHandler;
 
import cz.msebera.android.httpclient.Header;
 
public abstract class BaseResponse extends TextHttpResponseHandler {
 
    Context context;
    Dialog dialog;
 
    public BaseResponse(Context context) {
        this.context = context;
    }
 
    public abstract void onResponse(String jsonString);
 
    @Override
    public void onStart() {
        super.onStart();
 
        if (dialog == null) {
            dialog = new Dialog(context);
            Window window = dialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setDimAmount(0);
            dialog.setContentView(new ProgressBar(context));
            dialog.show();
        }
    }
 
    @Override
    public void onFinish() {
        super.onFinish();
 
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
 
    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
        this.onResponse(responseString);
    }
 
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        String errMsg = statusCode + " Error - " + throwable.getLocalizedMessage();
        Toast.makeText(context, errMsg, Toast.LENGTH_SHORT).show();
    }
}
 



통신 결과 JSON Parse

AsyncHttpClient client;
JsonResponse response;
 
/** Inner Class로 생성(클래스명 맘대로) */
class JsonResponse extends  BaseResponse {
    public JsonResponse(Context context) {
        super(context);
    }
 
    @Override
    public void onResponse(String jsonString) {
        Log.d("JsonString", jsonString);
 
        Gson gson = new Gson();
 
        /** Gson으로 Json Parse */
        Phone phone = gson.fromJson(jsonString, Phone.class);
 
        /** 양파까기 */
        textView1.setText("제품명 : "  + phone.item.name);
        textView2.setText("제품구분 : "  + phone.item.type);
        textView3.setText("출고가 : "  + phone.item.price.fixed);
        textView4.setText("판매가 : "  + phone.item.price.sale);
 
        /** ImageView에 값 설정 */
        Glide.with(MainActivity.this)
                .load(phone.item.img)
                .thumbnail(0.1f)
                .into(imageView);
    }
}



반응형

'Mobile > Android' 카테고리의 다른 글

retrofit2 + okhttp3 + recyclerview + infinite scroll  (0) 2021.01.05
Android HASHKEY 얻는 법  (0) 2019.12.21
Dialog ProgressBar  (0) 2019.12.15