import org.glassfish.grizzly.http.Method;
import org.glassfish.grizzly.http.util.Parameters;
import org.glassfish.grizzly.http.util.QueryStringBuilder;
import org.glassfish.grizzly.http.util.StringBuilderUtils;
import org.glassfish.grizzly.http.util.URIUtils;
import org.glassfish.grizzly.http.util.URLDecoder;
import org.glassfish.grizzly.http.util.URLEncoder;
import org.glassfish.grizzly.http.util.Utils;
import org.glassfish.grizzly.memory.ByteBufferWrapper;
import org.glassfish.grizzly.memory.MemoryManager;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
public class WeatherClient {
private static final String API_URL = "http://api.openweathermap.org/data/2.5/weather";
public static void main(String[] args) throws URISyntaxException, UnsupportedEncodingException {
GrizzlyAsyncHttpProvider provider = new GrizzlyAsyncHttpProvider();
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
.setWebSocketIdleTimeoutInMs(-1L)
.setRequestTimeoutInMs(60000L)
.build();
AsyncHttpClient client = new AsyncHttpClient(provider, config);
String cityName = "London";
QueryStringBuilder queryBuilder = new QueryStringBuilder();
queryBuilder.add("q", encode(cityName));
queryBuilder.add("appid", "YOUR_API_KEY"); // Replace with your own API key
client.prepareGet(API_URL + queryBuilder)
.execute(new AsyncCompletionHandler<String>() {
@Override
public String onCompleted(Response response) throws Exception {
String responseBody = response.getResponseBody();
System.out.println(responseBody);
return responseBody;
}
});
client.close();
}
private static String encode(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(Utils.urlEncode(value), Charset.forName("UTF-8").name());
}
}