com.shinylife.smalltools.api.ApiImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.shinylife.smalltools.api.ApiImpl.java

Source

package com.shinylife.smalltools.api;

import java.net.URLEncoder;
import java.util.HashMap;

import org.json.JSONObject;

import android.util.Log;

import com.shinylife.smalltools.entity.AddressInfo;
import com.shinylife.smalltools.entity.AppUpdateInfo;
import com.shinylife.smalltools.entity.IDCardInfo;
import com.shinylife.smalltools.entity.PhoneInfo;
import com.shinylife.smalltools.helper.Constants;
import com.shinylife.smalltools.helper.HttpClientHelper;
import com.shinylife.smalltools.helper.HttpException;
import com.shinylife.smalltools.helper.HttpHelp;
import com.shinylife.smalltools.helper.HttpResponse;

public class ApiImpl {
    public AddressInfo getAddressInfo(String no) {
        try {
            String response = HttpRequest(String.format(Constants.API_URL, "zip", URLEncoder.encode(no)));
            if (response != null && response.length() > 0) {
                AddressInfo ai = new AddressInfo();
                JSONObject jb = new JSONObject(response);
                ai.setCity(jb.getString("city"));
                ai.setLocation(jb.getString("location"));
                ai.setPhone(jb.getString("phone"));
                ai.setZipcode(jb.getString("zipcode"));
                ai.setProvince(jb.getString("province"));
                return ai;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    public HashMap getPigInfo(String no) {
        try {
            Log.v("vv", "starting.............");
            //String response =  HttpRequest(String.format(Constants.API_URL,"pig", URLEncoder.encode(no)));
            //HttpHelp httphelp=new HttpHelp("http://10.0.2.2:8080/test.jsp");
            HttpHelp httphelp = new HttpHelp("http://119.60.2.40:8080/esale/admin/farm.jsp");
            String response = httphelp.httpGet();
            Log.v("test...", response);
            if (response != null && response.length() > 0) {
                HashMap ai = new HashMap();
                JSONObject jb = new JSONObject(response);
                ai.put("code", jb.getString("code"));
                ai.put("dealer", jb.getString("dealer"));
                ai.put("dealeraddress", jb.getString("dealeraddress"));
                ai.put("wholesaler", jb.getString("wholesaler"));
                ai.put("slaughtercom", jb.getString("slaughtercom"));
                ai.put("slaughterdate", jb.getString("slaughterdate"));
                ai.put("mealinspect", jb.getString("mealinspect"));
                ai.put("animalspect", jb.getString("animalspect"));
                ai.put("pigsuppliers", jb.getString("pigsuppliers"));
                ai.put("pigorigin", jb.getString("pigorigin"));
                ai.put("pigorigin_id", jb.getString("pigorigin_id"));
                ai.put("transportcom", jb.getString("transportcom"));
                ai.put("transportlicense", jb.getString("transportlicense"));
                return ai;
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            Log.v("error", e.toString());
        }
        return null;
    }

    public IDCardInfo getIDCardInfo(String no) {
        try {
            String response = HttpRequest(String.format(Constants.API_URL, "id", no));
            if (response != null && response.length() > 0) {
                IDCardInfo ai = new IDCardInfo();
                JSONObject jb = new JSONObject(response);
                ai.setBirthday(jb.getString("birthday"));
                ai.setCode(jb.getString("code"));
                ai.setGender(jb.getString("gender").equals("m") ? "" : "");
                ai.setLocation(jb.getString("location"));
                return ai;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    public PhoneInfo getPhoneInfo(String no) {
        try {
            String response = HttpRequest(String.format(Constants.API_URL, "mobile", no));
            if (response != null && response.length() > 0) {
                PhoneInfo ai = new PhoneInfo();
                JSONObject jb = new JSONObject(response);
                ai.setPhonenum(jb.getString("phonenum"));
                ai.setLocation(jb.getString("location"));
                return ai;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    public AppUpdateInfo getNewVersion(String curVersionNo) {
        try {
            String response = HttpRequest(String.format(Constants.CHECK_VERSION_API_URL, curVersionNo));
            if (response != null && response.length() > 0) {
                AppUpdateInfo ai = new AppUpdateInfo();
                JSONObject jb = new JSONObject(response);
                ai.setHasNewVersion(jb.getBoolean("hasnewversion"));
                ai.setLastVersion(jb.getString("lastversion"));
                ai.setUrl(jb.getString("appurl"));
                ai.setAppSize(jb.getString("appsize"));
                return ai;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    private String HttpRequest(String url) throws HttpException {
        HttpClientHelper client = new HttpClientHelper();
        HttpResponse response = client.get(url, false);
        if (response != null) {
            return response.asString();
        }
        return null;
    }
}