高德定位API

package com.dazzle.XXXX.util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.json.JSONObject;
import org.json.JSONException;
import org.json.JSONArray;

import android.content.Context;
import android.util.Log;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;

public class WeatherGaoDeAPI {
    private static String TAG = "WeatherGaoDeAPI";
    private Context mContext;

    private final String URL_key = "&key=";
    private final String web_api_key_value = "";
    private final String URL_gzip = "&gzip=n";
    private final static String URL_GaoDe_key = "";

    private final static String KEY = URL_GaoDe_key;

    public WeatherGaoDeAPI(Context context){
        this.mContext = context;
    }

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }
    private static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
      
        String Result="";
        HttpGet httpRequest = new HttpGet(url);
        Log.v(TAG," url:"+url);
        Log.v(TAG," httpRequest:"+httpRequest);
        try {
            HttpClient httpClient = new DefaultHttpClient();
            Log.v(TAG,"xxx test httpClient:"+httpClient);
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            Log.v(TAG,"xxx test httpResponse:"+httpResponse);
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // Get returned data
                Log.v(TAG,"xxx test httpResponse.getStatusLine().getStatusCode():"+httpResponse.getStatusLine().getStatusCode());
                Result = EntityUtils.toString(httpResponse.getEntity());
                Log.v(TAG,"xxx test Result:"+Result);
            }
        } catch (Exception e) {
            Log.v(TAG,"xxx test  err  ++++++"+e);
            //JSONObject json = new JSONObject("");
            //return json;
        }
        JSONObject json = new JSONObject(Result);
        return json;
    }
    public static String getCityCodeByIp(String ipAddress) {
        JSONObject json = null;
        String adCode = "";
        try {
            json = readJsonFromUrl("http://restapi.amap.com/v5/ip?ip=" + ipAddress + "&key=" + KEY + "&type=4");
            if ("0".equals(json.getString("status"))){      //调用异常时,抛出返回信息,以便分析(我会在错误层统一捕获并输出到error日志)
                //throw new FailException("调用高德返回异常: " + json.toJSONString());
                Log.e(TAG,"调用高德返回异常: " + json.toString());
            }
            adCode = json.getString("city");          //获取ip定位的城市编码(高德返回json如下,需要其他也可自行获取)
            //adCode = json.getString("adcode");          //获取ip定位的城市编码(高德返回json如下,需要其他也可自行获取)
        }catch (JSONException e) {
            Log.e(TAG,"调用高德返回异常: "+e);
        }catch (IOException e) {
            e.printStackTrace();
            //throw new FailException("高德获取城市编码:", e);
            Log.e(TAG,"高德获取城市编码:"+e);
        }
        return adCode;
    }

    /*public static void main(String[] args) {
        String s = GdUtil.getCityCodeByIp("60.191.12.10");
        System.out.println(s);                             //330100

    }*/


    /**
     * 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     * @throws
     */
    
    public static String getLocationIP(){
        String URL_location = "http://pv.sohu.com/cityjson?ie=utf-8";
        String locationIP = "";
        String Location_Result="";
        HttpGet httpRequest_location = new HttpGet(URL_location);
        Log.v("xxx test"," URL_location:"+URL_location);
        // Get the httpresponse object
        try {
            HttpClient httpClient_location = new DefaultHttpClient();
            HttpResponse httpResponse_location = httpClient_location.execute(httpRequest_location);
            if (httpResponse_location.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //Get returned data
                Log.v("xxx test"," httpResponse_location.getStatusLine().getStatusCode():"+httpResponse_location.getStatusLine().getStatusCode());
                Location_Result = EntityUtils.toString(httpResponse_location.getEntity());
                //Log.v("xxx test"," Location_Result:"+Location_Result);
            }
        } catch (Exception e) {
            Log.v("xxx test getLocation err  ++++++",""+e);
            return null;
        }
        //returned JSON data
        if( null!=Location_Result  && !"".equals(Location_Result)){
            try {
                //Log.v("xxx  test "," 00000 "+Location_Result);
                int index = Location_Result.indexOf("{");
                locationIP = new JSONObject(Location_Result.substring(index)).getString("cip");
                Log.v("xxx test"," location=" +locationIP);
            } catch (JSONException e) {
                Log.v("xxx test"," getLocation: err "+e);
                return null;
            }
        }
        Log.v("xxx test"," getLocation() done ==="+locationIP);
        return locationIP;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值