如何从基于Java的Android应用程序中的API获取数据

xghobddn  于 2023-03-27  发布在  Android
关注(0)|答案(1)|浏览(88)

我试图通过使用跟踪端点链接从Jamendo API获取数据,以仅获取瑜伽音乐,然后将其显示给另一个Activity。

  • 我做了一个MusicListScreen,它有显示从API获取的数据的适配器。
  • YogaTracks类具有我应该获得的json响应的属性。
  • JSON响应:
{
  "headers":{
    "status":"success",
    "code":0,
    "error_message":"",
    "warnings":"",
    "results_count":2
  },
  "results":[
    {
      "id":"1532771",
      "name":"Let Me Hear You I",
      "duration":54,
      "artist_id":"461414",
      "artist_name":"Paul Werner",
      "artist_idstr":"PaulWernerMusic",
      "album_name":"Let Me Hear You I",
      "album_id":"404140",
      "license_ccurl":"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/",
      "position":1,
      "releasedate":"2018-03-15",
      "album_image":"https:\/\/usercontent.jamendo.com?type=album&id=404140&width=300&trackid=1532771",
      "audio":"https:\/\/prod-1.storage.jamendo.com\/?trackid=1532771&format=mp31&from=app-devsite",
      "audiodownload":"https:\/\/prod-1.storage.jamendo.com\/download\/track\/1532771\/mp32\/",
      "prourl":"https:\/\/licensing.jamendo.com\/track\/1532771",
      "shorturl":"https:\/\/jamen.do\/t\/1532771",
      "shareurl":"https:\/\/www.jamendo.com\/track\/1532771",
      "waveform":"{
        \"peaks\":[
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          10,
          12,
          7,
          7,
          1,
          5,
          1,
          7,
          2,
          10,
          13,
          9,
          8,
          1,
          3,
          1,
          4,
          6,
          3,
          14,
          9,
          7,
          7,
          6,
          4,
          5,
          16,
          11,
          16,
          4,
          4,
          2,
          2,
          8,
          7,
          10,
          11,
          17,
          18,
          11,
          6,
          5,
          2,
          0,
          11,
          9,
          14,
          8,
          2,
          3,
          1,
          14,
          12,
          9,
          1,
          1,
          2,
          0,
          3,
          0,
          11,
          10,
          10,
          12,
          14,
          24,
          49,
          24,
          11,
          8,
          8,
          17,
          17,
          9,
          20,
          21,
          17,
          16,
          11,
          9,
          9,
          15,
          12,
          18,
          7,
          2,
          4,
          1,
          22,
          19,
          14,
          5,
          5,
          2,
          0,
          6,
          0,
          14,
          10,
          10,
          13,
          15,
          16,
          9,
          13,
          12,
          5,
          9,
          10,
          10,
          6,
          23,
          18,
          13,
          12,
          15,
          3,
          1,
          20,
          12,
          22,
          6,
          7,
          2,
          3,
          14,
          11,
          9,
          1,
          13,
          10,
          7,
          6,
          23,
          47,
          33,
          29,
          23,
          20,
          14,
          7,
          5,
          7,
          10,
          5,
          2,
          0,
          19,
          74,
          70,
          41,
          17,
          27,
          30,
          30,
          24,
          22,
          75,
          69,
          54,
          20,
          18,
          26,
          29,
          26,
          28,
          49,
          70,
          61,
          34,
          16,
          27,
          32,
          33,
          25,
          50,
          70,
          70,
          32,
          15,
          28,
          27,
          29,
          25,
          45,
          88,
          64,
          33,
          15,
          29,
          30,
          30,
          24,
          30,
          69,
          74,
          49,
          23,
          31,
          38,
          39,
          34,
          29,
          80,
          81,
          55,
          22,
          22,
          28,
          28,
          30,
          27,
          62,
          73,
          60,
          28,
          16,
          28,
          28,
          30,
          22,
          56,
          72,
          65,
          37,
          14,
          33,
          30,
          27,
          21,
          37,
          71,
          65,
          39,
          13,
          28,
          31,
          28,
          25,
          24,
          67,
          79,
          54,
          23,
          18,
          26,
          27,
          24,
          19,
          60,
          69,
          55,
          24,
          19,
          27,
          29,
          29,
          21,
          65,
          75,
          59,
          24,
          20,
          33,
          28,
          26,
          23,
          40,
          74,
          64,
          32,
          14,
          62,
          29,
          27,
          19,
          27,
          83,
          78,
          43,
          13,
          29,
          30,
          27,
          24,
          27,
          74,
          69,
          56,
          14,
          17,
          29,
          29,
          25,
          25,
          64,
          26,
          19,
          17,
          12,
          12,
          10,
          8,
          7,
          55,
          74,
          62,
          25,
          17,
          30,
          30,
          26,
          21,
          34,
          69,
          73,
          47,
          18,
          21,
          29,
          26,
          26,
          31,
          71,
          77,
          50,
          16,
          25,
          27,
          28,
          28,
          23,
          88,
          74,
          48,
          10,
          21,
          27,
          32,
          23,
          19,
          64,
          87,
          66,
          38,
          19,
          24,
          31,
          26,
          21,
          59,
          84,
          73,
          32,
          17,
          26,
          27,
          28,
          26,
          50,
          84,
          68,
          41,
          15,
          24,
          29,
          28,
          21,
          26,
          76,
          71,
          56,
          14,
          22,
          29,
          26,
          23,
          18,
          76,
          74,
          59,
          20,
          14,
          27,
          26,
          23,
          20,
          45,
          77,
          60,
          32,
          16,
          26,
          29,
          26,
          25,
          48,
          73,
          67,
          31,
          19,
          29,
          30,
          27,
          25,
          35,
          100,
          66,
          39,
          11,
          23,
          25,
          26,
          22,
          20,
          79,
          79,
          47,
          17,
          23,
          34,
          34,
          27,
          23,
          72,
          82,
          58,
          22,
          19,
          27,
          28,
          27,
          30,
          59,
          55,
          60,
          32,
          23,
          27,
          28,
          24,
          20,
          52,
          68,
          72,
          52,
          22,
          16,
          23,
          23,
          21,
          32,
          73,
          30,
          30,
          42,
          35,
          28,
          20,
          20,
          22,
          70,
          58,
          17,
          28,
          37,
          35,
          27,
          24,
          22,
          66,
          62,
          21,
          31,
          38,
          35,
          29,
          26,
          24,
          63,
          68,
          24,
          29,
          36,
          40,
          32,
          24,
          20,
          45,
          88,
          51,
          24,
          22,
          30,
          34,
          29,
          20,
          27,
          98,
          68,
          49,
          15,
          21,
          27,
          27,
          27,
          29,
          80,
          78,
          55,
          24,
          19,
          28,
          29,
          23,
          19,
          72,
          73,
          76,
          31,
          35,
          88,
          79,
          66,
          35,
          47,
          66,
          59,
          25,
          42,
          75,
          77,
          69,
          52,
          36,
          62,
          68,
          52,
          25,
          50,
          68,
          61,
          44,
          32,
          66,
          68,
          64,
          46,
          42,
          84,
          71,
          64,
          36,
          63,
          63,
          62,
          34,
          39,
          75,
          58,
          67,
          69,
          62,
          35,
          34,
          82,
          54,
          42,
          25,
          52,
          77,
          54,
          34,
          32,
          76,
          58,
          58,
          33,
          32,
          32,
          44,
          39,
          21,
          31,
          12,
          33,
          22,
          34,
          24,
          27,
          61,
          57,
          50,
          19,
          24,
          31,
          27,
          25,
          21,
          63,
          69,
          59,
          23,
          15,
          33,
          27,
          26,
          21,
          45,
          66,
          62,
          31,
          20,
          40,
          34,
          33,
          35,
          46,
          62,
          69,
          42,
          18,
          33,
          27,
          26,
          26,
          32,
          75,
          59,
          45,
          22,
          30,
          30,
          28,
          25,
          23,
          66,
          72,
          53,
          27,
          22,
          32,
          29,
          27,
          20,
          62,
          79,
          54,
          20,
          22,
          39,
          33,
          30,
          29,
          56,
          68,
          61,
          36,
          16,
          34,
          31,
          29,
          25,
          46,
          53,
          62,
          42,
          19,
          33,
          28,
          30,
          25,
          32,
          70,
          66,
          48,
          16,
          27,
          30,
          29,
          22,
          19,
          57,
          65,
          57,
          38,
          30,
          37,
          29,
          30,
          27,
          55,
          72,
          62,
          29,
          22,
          34,
          27,
          27,
          24,
          51,
          78,
          61,
          32,
          17,
          33,
          29,
          28,
          23,
          34,
          65,
          68,
          45,
          14,
          59,
          35,
          30,
          29,
          27,
          72,
          72,
          45,
          17,
          34,
          33,
          29,
          28,
          30,
          62,
          62,
          56,
          30,
          22,
          36,
          32,
          26,
          27,
          58,
          50,
          53,
          28,
          15,
          35,
          30,
          26,
          21,
          50,
          68,
          63,
          38,
          18,
          30,
          27,
          27,
          23,
          27,
          64,
          70,
          52,
          19,
          27,
          33,
          31,
          32,
          32,
          63,
          73,
          55,
          18,
          24,
          29,
          28,
          31,
          26,
          63,
          71,
          60,
          22,
          19,
          32,
          29,
          28,
          21,
          52,
          70,
          63,
          34,
          17,
          36,
          31,
          30,
          22,
          48,
          75,
          68,
          35,
          22,
          33,
          29,
          26,
          26,
          40,
          67,
          63,
          51,
          20,
          29,
          34,
          30,
          29,
          26,
          61,
          59,
          51,
          19,
          30,
          38,
          32,
          25,
          20,
          61,
          67,
          61,
          28,
          18,
          35,
          29,
          26,
          23,
          41,
          73,
          64,
          38,
          19,
          33,
          30,
          31,
          34,
          44,
          64,
          65,
          40,
          28,
          34,
          29,
          32,
          28,
          25,
          70,
          66,
          42,
          22,
          31,
          31,
          29,
          26,
          22,
          61,
          71,
          51,
          29,
          39,
          47,
          32,
          32,
          31,
          58,
          73,
          66,
          29,
          16,
          39,
          28,
          28,
          25,
          55,
          68,
          56,
          31,
          16,
          29,
          20,
          18,
          18,
          17,
          8,
          3,
          2,
          6,
          3,
          1,
          1,
          1,
          0,
          1,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          10,
          15,
          8,
          13,
          9,
          3,
          2,
          1,
          1,
          4,
          18,
          13,
          14,
          11,
          5,
          2,
          2,
          1,
          43,
          38,
          26,
          23,
          22,
          21,
          20,
          19,
          13,
          12,
          13,
          12,
          10,
          7,
          4,
          1,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0
        ]
      }      ",

MusicListScrenn类

package com.minhhoang.yolo_yoga;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonArray;
import com.minhhoang.yolo_yoga.adapters.AdapterHome;
import com.minhhoang.yolo_yoga.adapters.AdapterMusicList;
import com.minhhoang.yolo_yoga.databasehelper.DataManager;
import com.minhhoang.yolo_yoga.models.ModelBlogCategory;
import com.minhhoang.yolo_yoga.models.ModelMainCategory;
import com.minhhoang.yolo_yoga.models.ModelMusicCategory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import JamendoAPI.JamendoApiService;
import JamendoAPI.YogaTracks;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MusicListScreen extends AppCompatActivity {

    RecyclerView recMainCategory;
    DataManager dataManager;
    ArrayList<YogaTracks> mainCategoryList;
    AdapterMusicList adapterMainCategory;
    TextView tvtitle;



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

        mainCategoryList = new ArrayList<YogaTracks>();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.jamendo.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        JamendoApiService api = retrofit.create(JamendoApiService.class);

        Call<YogaTracks> call = api.getYogaTracks("db9569db", "jsonpretty", "meditation");

        call.enqueue(new Callback<YogaTracks>() {
            @Override
            public void onResponse(Call<YogaTracks> call, Response<YogaTracks> response) {
                if(response.isSuccessful()){

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            YogaTracks obj = new YogaTracks();
                            obj.setAlbum_id(jsonObject.getString("album_id"));
                            obj.setName(jsonObject.getString("name"));
                            mainCategoryList.add(obj);

                            Log.e("Verify", obj.getAlbum_id() );

                        }
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }

                    Log.e("MyApp", "API call successful" );
                }

            }

            @Override
            public void onFailure(Call<YogaTracks> call, Throwable t) {

                Log.e("MyApp", "API call failed" + t.getMessage());
            }
        });


        init();
        Intent intent=getIntent();
        String tit= intent.getStringExtra("title");
        tvtitle.setText(tit);

        LinearLayoutManager llm = new LinearLayoutManager(this);
        adapterMainCategory = new AdapterMusicList(mainCategoryList,MusicListScreen.this);
        recMainCategory.setLayoutManager(llm);
        recMainCategory.setAdapter(adapterMainCategory);

    }
    private void init() {
        recMainCategory = findViewById(R.id.recMainCategory);
        tvtitle = findViewById(R.id.textView1);
    }

    public void backClick(View view) {
        finish();
    }
}

API接口

package JamendoAPI;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface JamendoApiService
{
    @GET("https://api.jamendo.com/v3.0/tracks/?client_id=db9569db&format=jsonpretty&tags=yoga")
    Call<YogaTracks>getYogaTracks(@Query("db9569db") String clientId,
                                               @Query("jsonpretty") String format,
                                               @Query("yoga") String tags);

}

YogaTracks课程

package JamendoAPI;

import java.io.Serializable;
import java.util.List;

public class YogaTracks implements Serializable {

   private List<YogaTracks> musics;

    private String id;

    private String name;

    public List<YogaTracks> getMusics() {
        return musics;
    }

    public void setMusics(List<YogaTracks> musics) {
        this.musics = musics;
    }

    private int duration;

    private String artist_id;

    private String artist_name;

    private String artist_idstr;

    private String album_name;

    private String album_id;
    private String license_ccurl;
    private int position;

    private String releasedate;

    private String album_image;

    private String audio;

    private String audiodownload;

    private String prourl;

    private String shorturl;

    private String shareurl;

    private String waveform;

    private String image;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getArtist_id() {
        return artist_id;
    }

    public void setArtist_id(String artist_id) {
        this.artist_id = artist_id;
    }

    public String getArtist_name() {
        return artist_name;
    }

    public void setArtist_name(String artist_name) {
        this.artist_name = artist_name;
    }

    public String getArtist_idstr() {
        return artist_idstr;
    }

    public void setArtist_idstr(String artist_idstr) {
        this.artist_idstr = artist_idstr;
    }

    public String getAlbum_name() {
        return album_name;
    }

    public void setAlbum_name(String album_name) {
        this.album_name = album_name;
    }

    public String getAlbum_id() {
        return album_id;
    }

    public void setAlbum_id(String album_id) {
        this.album_id = album_id;
    }

    public String getLicense_ccurl() {
        return license_ccurl;
    }

    public void setLicense_ccurl(String license_ccurl) {
        this.license_ccurl = license_ccurl;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getReleasedate() {
        return releasedate;
    }

    public void setReleasedate(String releasedate) {
        this.releasedate = releasedate;
    }

    public String getAlbum_image() {
        return album_image;
    }

    public void setAlbum_image(String album_image) {
        this.album_image = album_image;
    }

    public String getAudio() {
        return audio;
    }

    public void setAudio(String audio) {
        this.audio = audio;
    }

    public String getAudiodownload() {
        return audiodownload;
    }

    public void setAudiodownload(String audiodownload) {
        this.audiodownload = audiodownload;
    }

    public String getProurl() {
        return prourl;
    }

    public void setProurl(String prourl) {
        this.prourl = prourl;
    }

    public String getShorturl() {
        return shorturl;
    }

    public void setShorturl(String shorturl) {
        this.shorturl = shorturl;
    }

    public String getShareurl() {
        return shareurl;
    }

    public void setShareurl(String shareurl) {
        this.shareurl = shareurl;
    }

    public String getWaveform() {
        return waveform;
    }

    public void setWaveform(String waveform) {
        this.waveform = waveform;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

我得到了这个错误。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.minhhoang.yolo_yoga, PID: 24210
    java.lang.RuntimeException: org.json.JSONException: Not a primitive array: class retrofit2.Response
        at com.minhhoang.yolo_yoga.MusicListScreen$1.onResponse(MusicListScreen.java:86)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: org.json.JSONException: Not a primitive array: class retrofit2.Response
        at org.json.JSONArray.<init>(JSONArray.java:118)
        at com.minhhoang.yolo_yoga.MusicListScreen$1.onResponse(MusicListScreen.java:73)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
rdrgkggo

rdrgkggo1#

MusicListScrenn类-第73行:

JSONArray jsonArray = new JSONArray(response);

你需要访问主体,而不仅仅是“响应”,使用“.body()”,然后你有另一个问题,YogaTracks不是一个数组,所以你首先需要使用第一个JSONObject解构...
后来你会发现更多的问题,因为你的响应有两个字段(标题,结果)和你的计数,你已经在“结果”,所以你还需要处理,例如创建一个类“YogaTrackResponse”与这两个字段“标题”和“结果”(列表)和改变API接口的调用

相关问题