java中的HttpGet库

时间:2013-10-06 07:58:44

标签: java http-get

我有这段代码:

package sig;

import org.json.JSONObject;

public class MyService extends java.lang.Object{

    public JSONObject getLocationInfo( double lat, double lng) {

        HttpGet httpGet = new HTTP("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}

HttpGet中的错误,我应该使用哪个库才能使用HttpGet,谢谢!

1 个答案:

答案 0 :(得分:0)

使用httpcomponents-client jar。

它有Maven条目:

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.1</version>
    </dependency>