Reputation: 11
I am building an app on Android Studio in which I have to keep the currency rates synchronized with an external source through a JSON object. I have followed this tutorialwhich was given to me by another answer on this forum. The source seems reliable. However, I keep getting a NoClassDefError. I have put a flag marked as //Exception in my code to show you where the exception occurs. Note that i have added httpclient-4.3-beta1.jar as a library so I don't think it's a dependency problem. I have found topics about the same issue but they only provide explanations and no answers. This is my code. Any help would be much appreciated! Thank you.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class LiveResponseDemo{
// essential URL structure is built using constants
public static final String ACCESS_KEY = "YOUR_ACCESS_KEY";
public static final String BASE_URL = "https://apilayer.net/api/";
public static final String ENDPOINT = "live";
// this object is used for executing requests to the (REST) API
//Exception
static CloseableHttpClient httpClient = HttpClients.createDefault();
public static void sendLiveRequest(){
// The following line initializes the HttpGet Object with the URL in order to send a request
HttpGet get = new HttpGet(BASE_URL + ENDPOINT + "?access_key=" + ACCESS_KEY);
try {
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
// the following line converts the JSON Response to an equivalent Java Object
JSONObject exchangeRates = new JSONObject(EntityUtils.toString(entity));
System.out.println("Live Currency Exchange Rates");
// Parsed JSON Objects are accessed according to the JSON resonse's hierarchy, output strings are built
Date timeStampDate = new Date((long)(exchangeRates.getLong("timestamp")*1000));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String formattedDate = dateFormat.format(timeStampDate);
System.out.println("1 " + exchangeRates.getString("base") + " in GBP : " + exchangeRates.getJSONObject("rates").getDouble("GBP") + " (as of " + formattedDate + ")");
System.out.println("\n");
response.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 2255
Reputation: 2690
It's not the correct answer on his question, but the solution that worked for him.
The solution I suggested is to keep on the standard and follow the advice from google. As described in the API docs: http://developer.android.com/reference/org/apache/http/client/HttpClient.html
We should use URLConnection as replacement for apache httpclient. As described here: http://developer.android.com/reference/java/net/URLConnection.html
Upvotes: 2