通过 Android Studio NoClassDefFoundError 的 HTTP 请求
HTTP request via Android Studio NoClassDefFoundError
我正在 Android Studio 上构建一个应用程序,我必须在其中通过 JSON 对象使货币汇率与外部源保持同步。我已关注 this tutorial,这是该论坛上另一个答案给我的。消息来源似乎可靠。但是,我不断收到 NoClassDefError。我在我的代码中放置了一个标记为 //Exception 的标志,以向您显示异常发生的位置。请注意,我已将 httpclient-4.3-beta1.jar 添加为库,因此我认为这不是依赖性问题。我找到了关于同一问题的主题,但他们只提供解释而没有答案。这是我的代码。任何帮助将非常感激!谢谢。
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();
}
}
}
这不是他问题的正确答案,而是对他有效的解决方案。
- HttpClient 不再是 Android SDK 的一部分,并且已标记为已弃用。
- 使用带有 httpclient impl 的库 jar。在类路径中两次调用 httpclient 框架,这很难。
我建议的解决方案是保持标准并遵循 google 的建议。
如 API 文档中所述:http://developer.android.com/reference/org/apache/http/client/HttpClient.html
我们应该使用 URLConnection 来替代 apache httpclient。
如此处所述:
http://developer.android.com/reference/java/net/URLConnection.html
我正在 Android Studio 上构建一个应用程序,我必须在其中通过 JSON 对象使货币汇率与外部源保持同步。我已关注 this tutorial,这是该论坛上另一个答案给我的。消息来源似乎可靠。但是,我不断收到 NoClassDefError。我在我的代码中放置了一个标记为 //Exception 的标志,以向您显示异常发生的位置。请注意,我已将 httpclient-4.3-beta1.jar 添加为库,因此我认为这不是依赖性问题。我找到了关于同一问题的主题,但他们只提供解释而没有答案。这是我的代码。任何帮助将非常感激!谢谢。
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();
}
}
}
这不是他问题的正确答案,而是对他有效的解决方案。
- HttpClient 不再是 Android SDK 的一部分,并且已标记为已弃用。
- 使用带有 httpclient impl 的库 jar。在类路径中两次调用 httpclient 框架,这很难。
我建议的解决方案是保持标准并遵循 google 的建议。 如 API 文档中所述:http://developer.android.com/reference/org/apache/http/client/HttpClient.html
我们应该使用 URLConnection 来替代 apache httpclient。 如此处所述: http://developer.android.com/reference/java/net/URLConnection.html