Sign in | Sign up

Java

Tagged:

Currently contains 2 posts

Avatar

April 16, 2015 04:14

kwDev

I am getting a 500 error when trying to connect using Java and Apache

Avatar

April 16, 2015 04:16

kwDev

Code:

public class httpGet{

public static ArrayList<NutritionData> httpGetData(String url) {

ArrayList<NutritionData> dataFromService = new ArrayList<NutritionData>(); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair(“appId”, “781dbb8d”)); nameValuePairs.add(new BasicNameValuePair(“appKey”, “e77d941048fab5a248b15903bf9fdca4”)); try ( CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { String paramsString = URLEncodedUtils.format(nameValuePairs, “UTF-8”); HttpGet request = new HttpGet(url + “?” + paramsString); request.addHeader(“content-type”, “application/json”); request.addHeader(“connection”, “keep-alive”); HttpResponse result = httpClient.execute(request); System.out.print(result); String json = EntityUtils.toString(result.getEntity(), “UTF-8”); com.google.gson.Gson gson = new com.google.gson.Gson(); NutritionData response = gson.fromJson(json, NutritionData.class); System.out.print(response); } catch (IOException ex) { ex.getMessage(); } return dataFromService;

}
}

Reply to thread