我在我的Android应用程序中使用HTTP核心和客户端jar。我在SDK 23上运行。 我没有得到任何编译时错误。应用程序运行,直到调用getRally方法,该方法使用外部依赖项。
我的mainactivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
public class MainActivity extends AppCompatActivity
{
String host = "https://rally1.rallydev.com";
String applicationName = "Android-Rally";
RallyRestApi restApi;
Button buttonlogin;
EditText etemail,etpass;
QueryRequest qtestset;
QueryResponse response;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etemail=(EditText) findViewById(R.id.etemail);
etpass=(EditText) findViewById(R.id.etpassword);
buttonlogin=(Button) findViewById(R.id.btnlogin);
buttonlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(etemail.equals("") && etpass.equals(""))
{
Toast.makeText(getApplicationContext(),"Enter your Rally Credentials",Toast.LENGTH_LONG).show();
}
else
{
try
{
getRally();
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
qtestset = new QueryRequest("Defects");
qtestset.setLimit(1);
try
{
response = restApi.query(qtestset);
if(!response.wasSuccessful())
{
Toast.makeText(getApplicationContext(),"Login Un-Successful",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Login Successful",Toast.LENGTH_LONG).show();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
public RallyRestApi getRally() throws URISyntaxException {
String password = etemail.getText().toString();
String email=etpass.getText().toString();
restApi = new RallyRestApi(new URI(host), email,password);
restApi.setApplicationName(applicationName);
return restApi;
}
}
我的应用Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
packagingOptions
{
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/dependencies.txt'
}
defaultConfig {
applicationId "com.example.apetkar.rally_rest_android"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies
{
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile ('com.android.support:appcompat-v7:23.2.0')
compile files('libs/gson-2.2.4.jar')
compile files('libs/commons-logging-1.1.1.jar')
compile files('libs/httpcore-4.2.4.jar')
compile files('libs/httpclient-4.2.5.jar')
compile files('libs/rally-rest-api-2.2.1.jar')
}
错误记录
java.lang.NoSuchMethodError: No static method extractHost(Ljava/net/URI;)Lorg/apache/http/HttpHost; in class Lorg/apache/http/client/utils/URIUtils; or its super classes (declaration of 'org.apache.http.client.utils.URIUtils' appears in /system/framework/org.apache.http.legacy.boot.jar)
at org.apache.http.impl.client.DecompressingHttpClient.getHttpHost(DecompressingHttpClient.java:113)
at org.apache.http.impl.client.DecompressingHttpClient.execute(DecompressingHttpClient.java:108)
at com.rallydev.rest.client.HttpClient.executeRequest(HttpClient.java:157)
at com.rallydev.rest.client.HttpClient.doRequest(HttpClient.java:145)
at com.rallydev.rest.client.BasicAuthClient.doRequest(BasicAuthClient.java:56)
at com.rallydev.rest.client.HttpClient.doGet(HttpClient.java:221)
at com.rallydev.rest.RallyRestApi.query(RallyRestApi.java:172)
at com.example.apetkar.rally_rest_android.MainActivity$1.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
我确实调查了这个问题。问题是android的默认org.apache.http.client.utils.URIUtils
在运行时被调用,它不包含方法提取主机。方法extracthost在HttpClient.jar中..所以我怎么能强制app调用Jar中的类而不是默认的androids类?
答案 0 :(得分:1)
您正在使用旧的Apache HTTP客户端......而且还没有使用Android
compile group: 'org.apache.httpcomponents', name: 'httpclient-android', version: '4.3.5.1'
Simliarly,你真的不需要JAR文件,我不这么认为。
compile group: 'com.rallydev.rest', name: 'rally-rest-api', version: '2.2.1'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
如何强制应用程序调用Jar中的类而不是默认的androids类
我不确定Rally API库是否需要Apache HTTP,但我建议使用不同的HTTP库,因为在Android 23中删除了Apache HTTP,这是您编译的,所以代码不应该调用默认类。