我创建了一个基于mashape api调用的demo。但是我得到了错误。我尝试了很多建议。但是没有得到任何解决方案。
java代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CallMashapeAsync().execute();
}
private class CallMashapeAsync extends AsyncTask<String, Integer, HttpResponse<JsonNode>> {
protected HttpResponse<JsonNode> doInBackground(String... msg) {
HttpResponse<JsonNode> request = null;
try {
request = Unirest.get("https://indianpost.p.mashape.com/getOffices.php?pincode=394101")
.header("X-Mashape-Key", "My API")
.header("Accept", "application/json")
.asJson();
} catch (UnirestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return request;
}
protected void onProgressUpdate(Integer...integers) {
}
protected void onPostExecute(HttpResponse<JsonNode> response) {
String answer = response.getBody().toString();
TextView txtView = (TextView) findViewById(R.id.textview1);
txtView.setText(answer);
}
}
}
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.hungama3.myapplication"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile files('libs/gson-2.2.4.jar')
compile files('libs/httpclient-4.3-beta1.jar')
compile files('libs/mashape-java-client-2.0.10.jar')
compile files('libs/unirest-java-1.3.0.jar')
compile files('libs/org.apache.commons.logging-1.1.1.jar')
}
错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.impl.conn.PoolingHttpClientConnectionManager
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:459)
at com.mashape.unirest.http.options.Options.refresh(Options.java:41)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:27)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:138)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:121)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:64)
at com.example.hungama3.myapplication.MainActivity$CallMashapeAsync.doInBackground(MainActivity.java:33)
at com.example.hungama3.myapplication.MainActivity$CallMashapeAsync.doInBackground(MainActivity.java:27)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
我为http apache库尝试了很多jar文件,但没有得到任何解决方案。 如果有人有任何建议,请告诉我。
在try catch中指示“request”的错误焦点行37。
答案 0 :(得分:1)
再次修改:
经过第二次思考,实际上android的库已经包含了httpclient(pre android 6.0),你自己包含它,你可能会产生冲突。尝试简单地移除jar并尝试。
删除该行
compile files('libs/httpclient-4.3-beta1.jar')
您可能还需要将其删除
compile files('libs/org.apache.commons.logging-1.1.1.jar')
答案 1 :(得分:0)
您是否需要添加HttpComponents作为依赖项?不确定你是否需要像你一样包含HttpClient,android已经包含它了。
答案 2 :(得分:0)
对于android删除所有httpclient和httpcore依赖项并添加
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' // gradle depedencies