Created
September 5, 2016 07:17
-
-
Save kamikat/18be0970d93998a176c3538ddbdbf2ac to your computer and use it in GitHub Desktop.
RxJava implementation to locate user with http://ip.taobao.com/service/getIpInfo.php?ip=myip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import rx.Observable; | |
import rx.Scheduler; | |
import rx.Subscriber; | |
import rx.schedulers.Schedulers; | |
public class RxLocation implements Observable.OnSubscribe<RxLocation.GeoInfo> { | |
public static Observable<GeoInfo> create() { | |
return create(Schedulers.io()); | |
} | |
public static Observable<GeoInfo> create(Scheduler scheduler) { | |
return Observable.create(new RxLocation(scheduler)); | |
} | |
Scheduler mScheduler; | |
private RxLocation(Scheduler scheduler) { | |
mScheduler = scheduler; | |
} | |
@Override | |
public void call(Subscriber<? super GeoInfo> subscriber) { | |
mScheduler.createWorker().schedule(() -> { | |
try { | |
String jsonString = fetch(); | |
JSONObject json = new JSONObject(jsonString); | |
if (json.getInt("code") != 0) { | |
throw new AssertionError("GeoInfo request must success with code 0"); | |
} | |
json = json.getJSONObject("data"); | |
GeoInfo data = new GeoInfo(); | |
data.ip = json.getString("ip"); | |
data.country = json.getString("country"); | |
data.area = json.getString("area"); | |
data.region = json.getString("region"); | |
data.city = json.getString("city"); | |
data.isp = json.getString("isp"); | |
data.countryId = json.getString("country_id"); | |
data.areaId = json.getString("area_id"); | |
data.regionId = json.getString("region_id"); | |
data.cityId = json.getString("city_id"); | |
data.ispId = json.getString("isp_id"); | |
subscriber.onNext(data); | |
subscriber.onCompleted(); | |
} catch (Exception e) { | |
subscriber.onError(e); | |
} | |
}); | |
} | |
private String fetch() throws IOException { | |
HttpURLConnection conn = null; | |
try { | |
URL url = new URL("http://ip.taobao.com/service/getIpInfo2.php?ip=myip"); | |
conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setRequestProperty("Content-length", "0"); | |
conn.setUseCaches(false); | |
conn.setAllowUserInteraction(false); | |
conn.setConnectTimeout(500); | |
conn.setReadTimeout(4500); | |
conn.connect(); | |
int status = conn.getResponseCode(); | |
switch (status) { | |
case 200: | |
case 201: | |
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line).append("\n"); | |
} | |
br.close(); | |
return sb.toString(); | |
default: | |
throw new IOException("should always have status == 200"); | |
} | |
} finally { | |
if (conn != null) { | |
conn.disconnect(); | |
} | |
} | |
} | |
public static final class GeoInfo { | |
public String ip; | |
public String country; | |
public String area; | |
public String region; | |
public String city; | |
public String isp; | |
public String countryId; | |
public String areaId; | |
public String regionId; | |
public String cityId; | |
public String ispId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment