Android Asynchronous Http Client


android 의 비동기 HTTP 라이브러리 Asynchronous Http Client 에 대한 자료는

http://loopj.com/android-async-http/

http://yasirameen.com/2016/05/asynchronous-http-client-android/

https://java2android1108.blogspot.com/2018/01/android-web-services-using-android.html

를 참조하면 기본적인 사용법은 알 수 있다.

한글 설명은 http://edoli.tistory.com/91 를 참조하면 된다.

예제 설명이 잘된 자료는 http://yasirameen.com/2016/05/asynchronous-http-client-android/ 다.


아래 예제는 자동 로그인하는 Intro.java 코드다. 사용해보니 코드가 심플하고 편한 거 같다.

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.google.firebase.iid.FirebaseInstanceId;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.tistory.link2me.common.AES256Cipher;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import cz.msebera.android.httpclient.Header;

public class Intro extends AppCompatActivity {
    Boolean loginChecked;
    public SharedPreferences settings;
    String loginID;
    String loginPW;
    String getDeviceID; // 스마트기기의 장치 고유값

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);

        // 자동 로그인 체크 검사
        settings = getSharedPreferences("settings", Activity.MODE_PRIVATE);
        loginChecked = settings.getBoolean("LoginChecked", false);
        if (loginChecked) {
            loginID = settings.getString("loginID", "");
            loginPW = settings.getString("loginPW", "");
            if(loginID != null && !loginID.isEmpty() && loginPW != null && !loginPW.isEmpty()){
                AsyncHttpComm(loginID,loginPW);
            } else {
                startActivity(new Intent(getApplication(), Login.class));
                finish();
            }
        } else{
            startActivity(new Intent(getApplication(), Login.class));
            finish();
        }
    }

    public void AsyncHttpComm(String userID, String userPW){
        RequestParams params = new RequestParams();
        try {
            loginID = AES256Cipher.AES_Encode(userID, Value.AES256Key);
            loginPW = AES256Cipher.AES_Encode(userPW, Value.AES256Key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        // 단말기의 ID 정보를 얻기 위해서는 READ_PHONE_STATE 권한이 필요
        TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null){
            getDeviceID = mTelephony.getDeviceId();  // 스마트폰 기기 정보
        } else {
            getDeviceID = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        // 사용자 기기의 고유 토큰 정보를 획득
        String getToken = FirebaseInstanceId.getInstance().getToken();

        params.put("loginID", loginID);
        params.put("loginPW", loginPW);
        params.put("deviceID", getDeviceID);
        params.put("tokenID", getToken);
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(Value.IPADDRESS + "/loginChk.php", params, new TextHttpResponseHandler() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                System.out.println("statusCode : " + statusCode);
                System.out.println("responseString : " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                System.out.println("Response Value : " + responseString);
                if(Integer.parseInt(responseString) > 0){ // 로그인 정보 일치하면 uid 정보를 받음
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("idx", responseString);
                    Toast.makeText(Intro.this,"로그인 성공", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplication(), MainActivity.class));
                    finish(); // 현재 Activity 를 없애줌

                } else {
                    startActivity(new Intent(getApplication(), Login.class));
                    finish();
                }
            }
        });
    }

}



Link2Me

댓글을 달아 주세요

Powerd by Tistory, designed by criuce
rss