从mashapi获取数据(No Filed INASTANCE)

时间:2018-05-23 10:53:32

标签: android api android-studio mashape gson

我是mashapi的新手,并且不知道如何从mashapi获取数据 我添加所有jar以使用在mashapi中使用的Unirest库。

我收到错误 java.lang.NoSuchFieldError:没有类型Lorg / apache / http / conn / ssl / AllowAllHostnameVerifier的字段INSTANCE;

我也尝试在线找到解决方案,但无法解决我的错误

我被困了2天。

任何。!!

提前抢先!!!

这是我的Logcat

    05-23 15:30:52.291 6443-6443/? W/System: ClassLoader referenced unknown path: /data/app/com.creativeera.google.mutalfunds-2/lib/arm
05-23 15:30:52.310 6443-6443/? I/InstantRun: starting instant run server: is main process
05-23 15:30:52.415 6443-6443/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-23 15:30:52.605 6443-6535/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                 Process: com.creativeera.google.mutalfunds, PID: 6443
                                                 java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                     at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                     at java.lang.Thread.run(Thread.java:761)
                                                  Caused by: java.lang.NoSuchFieldError: No field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar:classes2.dex)
                                                     at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:146)
                                                     at org.apache.http.conn.ssl.SSLConnectionSocketFactory.getSocketFactory(SSLConnectionSocketFactory.java:174)
                                                     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:114)
                                                     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:121)
                                                     at com.mashape.unirest.http.options.Options.refresh(Options.java:72)
                                                     at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)
                                                     at com.mashape.unirest.http.options.Options.getOption(Options.java:42)
                                                     at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:151)
                                                     at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
                                                     at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
                                                     at com.creativeera.google.mutalfunds.MainActivity$getapi.doInBackground(MainActivity.java:50)
                                                     at com.creativeera.google.mutalfunds.MainActivity$getapi.doInBackground(MainActivity.java:41)
                                                     at android.os.AsyncTask$2.call(AsyncTask.java:305)

这是我的 AppGradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.creativeera.google.mutalfunds"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    useLibrary 'org.apache.http.legacy'
}

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:26.1.0'
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.code.gson:gson:2.8.4'
    androidTestCompile 'com.android.support:support-annotations:26.1.0'
    // gradle depedencies
    compile 'com.rapidapi:rapidconnect-android:0.1'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //implementation files('libs/httpasyncclient-4.1.3.jar')
    implementation files('libs/httpclient-4.5.5.jar')
    implementation files('libs/httpcore-4.4.9.jar')
    implementation files('libs/unirest-java-1.4.9.jar')
    implementation files('libs/json-20180130.jar')
    implementation files('libs/httpmime-4.5.5.jar')
}

这是 Project Gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven{
            url 'https://raw.github.com/zeeshanejaz/unirest-android/mvn-repo'
        }
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的java类,我简单地向api提出了一些请求这个错误。

    package com.creativeera.google.mutalfunds;

import android.app.LoaderManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.GetRequest;
import com.rapidapi.rapidconnect.Argument;
import com.rapidapi.rapidconnect.RapidApiConnect;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;
import org.json.HTTP;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new getapi().execute();
    }

    class getapi extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            try {
                HttpResponse<JsonNode> response = Unirest.get("https://mitanjos-india-mutual-fund-nav-prices-v1.p.mashape.com/navdata/list/?amfiId=118933")
                        .header("X-Mashape-Key", "LdDc1IaYgAmshWP79DV0X56ILzlXp16w5U2jsn9vDSLj942SZD")
                        .header("Accept", "application/json")
                        .asJson(); //getting error is this Line
            } catch (UnirestException e) {
                e.printStackTrace();
            }


            return null;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码。

try {
            HttpClient client = new DefaultHttpClient();
            //HttpPost post = new HttpPost("https://mitanjos-india-mutual-fund-nav-prices-v1.p.mashape.com/navdata/list/?amfiId=118547");
            HttpPost post = new HttpPost("https://mitanjos-india-mutual-fund-nav-prices-v1.p.mashape.com/fund/fundTypes");

            JSONObject json = new JSONObject();


            new StringEntity(json.toString());
            post.addHeader("X-Mashape-Key", "PUT YOU API KEY");
            post.addHeader("Accept", "application/json");

            // System.out.println("status code is:" + status);
            org.apache.http.HttpResponse response = client.execute(post);
            int status = response.getStatusLine().getStatusCode();
            Log.d("status", "" + status);
            if (status == 200) {
                InputStream in = new BufferedInputStream(response.getEntity().getContent());
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String line, result;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                result = sb.toString();
                JSONArray obj=new JSONArray(result);
               for (int i=0;i<obj.length();i++)
                {
                    //JSONObject jsonObject = obj.getJSONObject(i);
                    //String genresongtitile = jsonObject.getString("fundName");
                    Log.d("status", "" + obj);
                }

            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }