狗品种API Android Studio

时间:2018-05-05 07:04:00

标签: java android json api

这是link to the API for JSON

我正在尝试访问列表和狗品种的图片,但是当我的应用在Android Studio中启动时,它什么都没显示。

在主要活动类之后,我也给了CustomAdapter类。 尝试过其他事情但没有工作。

这是我的MainActivity类:

public class MainActivity extends AppCompatActivity {

    ArrayList<Product> arrayList;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arrayList = new ArrayList<>();
        list = (ListView) findViewById(R.id.list);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new ReadJSON().execute("https://dog.ceo/api/breeds/list/all");
            }
        });
    }

    class ReadJSON extends AsyncTask<String,Integer, String>{
        @Override
        protected String doInBackground(String... params) {
            return readURL(params[0]);
        }

        @Override
        protected void onPostExecute(String content) {
            try {
                JSONObject jsonObject = new JSONObject(content);
                JSONArray jsonArray =  jsonObject.getJSONArray("status");

                for(int i =0;i<jsonArray.length(); i++){
                    JSONObject productObject = jsonArray.getJSONObject(i);
                    arrayList.add(new Product(
                            productObject.getString("status"),
                            productObject.getString("message")
                    ));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            CustomAdapter adapter = new CustomAdapter(
                    getApplicationContext(), R.layout.list_row, arrayList
            );
            list.setAdapter(adapter);
        }
    }

    private static String readURL(String theUrl) {
        StringBuilder content = new StringBuilder();
        try {
            // create a url object
            URL url = new URL(theUrl);
            // create a urlconnection object
            URLConnection urlConnection = url.openConnection();
            // wrap the urlconnection in a bufferedreader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            // read from the urlconnection via the bufferedreader
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}

CustomerAdapter:

public class CustomAdapter extends ArrayAdapter<Product> {

    ArrayList<Product> products;
    Context context;
    int resource;

    public CustomAdapter(Context context, int resource, ArrayList<Product> products) {
        super(context, resource, products);
        this.products = products;
        this.context = context;
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) getContext()
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_row, null, true);

        }
        Product product = getItem(position);

        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        Picasso.with(context).load(product.getImage()).into(image);

        TextView breedName = (TextView) convertView.findViewById(R.id.breedName);
        breedName.setText(product.getBreedName());


        return convertView;
    }
}

产品:

enter code here

3 个答案:

答案 0 :(得分:1)

您需要对AsyncTask方法进行更改。您需要更改 Json Parsing 流程,如下所示。

更改您的ReadJSON课程,如下所示。

class ReadJSON extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... params) {
        return readURL(params[0]);
    }

    @Override
    protected void onPostExecute(String content) {
        try {
            Log.w("content", content);
            JSONObject jsonObject = new JSONObject(content);
            JSONObject messageObj = jsonObject.getJSONObject("message");

            for (Iterator<String> iter = messageObj.keys(); iter.hasNext(); ) {
                String key = iter.next();
                Log.w("key", key);
                arrayList.add(new Product("", key));
            }

            Log.w("messageObj", messageObj.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        CustomAdapter adapter = new CustomAdapter(
                TestActivity.this, R.layout.list_row, arrayList
        );
        list.setAdapter(adapter);
    }
}

我目前只制作了品种名称。 对于随机Image,您应该根据名称从相关的api获取。

CustomAdapter内,我现在只设置品种名称。

public class CustomAdapter extends ArrayAdapter<Product> {

    ArrayList<Product> products;
    Context context;
    int resource;

    public CustomAdapter(Context context, int resource, ArrayList<Product> products) {
        super(context, resource, products);
        this.products = products;
        this.context = context;
        this.resource = resource;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) getContext()
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_row, null, true);

        }
        Product product = getItem(position);

        /*ImageView image = (ImageView) convertView.findViewById(R.id.ivBreedDog);
        Picasso.with(context).load(product.getImage()).into(image);*/

        TextView breedName = (TextView) convertView.findViewById(R.id.breedName);
        breedName.setText(product.getBreedName());


        return convertView;
    }


}

<强>产品

public class Product {
    // Store the name of the movie
    private String image;
    // Store the release date of the movie
    private String breedName;

    public String getImage() {
        return image;
    }

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

    public String getBreedName() {
        return breedName;
    }

    public void setBreedName(String breedName) {
        this.breedName = breedName;
    }


    // Constructor that is used to create an instance of the Movie object
    public Product(String image, String breedName) {
        this.image = image;
        this.breedName = breedName;
    }
}

这只会在 breed name 中为您提供 list

breed name 中输出 list

enter image description here

答案 1 :(得分:1)

我对您的代码进行了更改,如下所示:

更新:感谢@Jay Rathod RJ的solution,我更新了代码,以便从服务器加载品种名称,然后根据品种名称加载图片。

创建BreedLoader类:

package com.example.pbp22.dogbreed;

import android.content.AsyncTaskLoader;
import android.content.Context;

import java.util.List;

public class BreedLoader extends AsyncTaskLoader<List<Breed>> {
/**
 * Tag for log messages
 */
private static final String LOG_TAG = BreedLoader.class.getName();

List<String> breedNames;

public BreedLoader(Context context, List<String> breedNames) {
    super(context);
    this.breedNames = breedNames;
}

@Override
protected void onStartLoading() {
    forceLoad();
}

/**
 * This is on a background thread.
 */
@Override
public List<Breed> loadInBackground() {

    // Perform the network request, parse the response, and extract a list of breeds.
    return QueryUtils.fetchBreedData(breedNames);
}
}

创建BreedNameLoader类:

package com.example.pbp22.dogbreed;

import android.content.AsyncTaskLoader;
import android.content.Context;

import java.util.List;

public class BreedNameLoader extends AsyncTaskLoader<List<String>> {

    /**
     * Tag for log messages
     */
    private static final String LOG_TAG = BreedLoader.class.getName();

    public BreedNameLoader(Context context) {
        super(context);
    }

    @Override
    protected void onStartLoading() {
        forceLoad();
    }

    /**
     * This is on a background thread.
     */
    @Override
    public List<String> loadInBackground() {

        // Perform the network request, parse the response, and extract a list of earthquakes.
        return QueryUtils.fetchBreedNameData();
    }
}

创建QueryUtils类:

    package com.example.pbp22.dogbreed;

import android.text.TextUtils;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class QueryUtils {

    /** Tag for the log messages */
    private static final String LOG_TAG = QueryUtils.class.getSimpleName();

    /**
     * Query the dataset and return a list of {@link Breed} objects.
     * @param breedNames
     */
    public static List<Breed> fetchBreedData(List<String> breedNames) {

        // Perform HTTP request to the URL and receive a JSON response back
        List<String> jsonResponse = readImageUrl(breedNames);

        // Extract relevant fields from the JSON response and create a list of {@link Breed}s
        List<Breed> breeds = extractBreedFromJson(jsonResponse, breedNames);

        // Return the list of {@link Breed}s
        return breeds;
    }

    /**
     * Query the dataset and return a list of {@link Breed} objects.
     */
    public static List<String> fetchBreedNameData() {

        // Perform HTTP request to the URL and receive a JSON response back
        String jsonResponse = readBreedNameUrl();

        // Extract relevant fields from the JSON response and create a list of {@link Breed}s
        List<String> breedNames = extractBreedNameFromJson(jsonResponse);

        // Return the list of {@link Breed}s
        return breedNames;
    }

    private static List<String> readImageUrl(List<String> breedNames) {

        List<String> reponses = new ArrayList<>();

        for (String breed : breedNames) {
            StringBuilder content = new StringBuilder();
            try {
                // create a url object
                URL url = new URL(String.format("https://dog.ceo/api/breed/%s/images/random", breed));
                // create a urlconnection object
                URLConnection urlConnection = url.openConnection();
                // wrap the urlconnection in a bufferedreader
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line;
                // read from the urlconnection via the bufferedreader
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line + "\n");
                }
                bufferedReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            reponses.add(content.toString());
        }

        return reponses;
    }

    /**
     * Return a list of {@link Breed} objects that has been built up from
     * parsing the given JSON response.
     */
    private static List<Breed> extractBreedFromJson(List<String> responses, List<String> breedNames) {

        // If the JSON string is empty or null, then return early.
        if (TextUtils.isEmpty(responses.toString())) {
            return null;
        }

        // Create an empty ArrayList that we can start adding breeds to
        List<Breed> breeds = new ArrayList<>();

        for (int i = 0; i < responses.size(); i++) {

            try {

                // Create a JSONObject from the JSON response string
                JSONObject baseJsonResponse = new JSONObject(responses.get(i));

                String image = baseJsonResponse.getString("message");

                String breedName = breedNames.get(i);

                breeds.add(new Breed(image, breedName));

            } catch (JSONException e) {
                // If an error is thrown when executing any of the above statements in the "try" block,
                // catch the exception here, so the app doesn't crash. Print a log message
                // with the message from the exception.
                Log.e("QueryUtils", "Problem parsing the JSON results", e);
            }


        }

        // Return the list of breeds
        return breeds;
    }

    private static String readBreedNameUrl() {

            StringBuilder content = new StringBuilder();
            try {
                // create a url object
                URL url = new URL("https://dog.ceo/api/breeds/list/all");
                // create a urlconnection object
                URLConnection urlConnection = url.openConnection();
                // wrap the urlconnection in a bufferedreader
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line;
                // read from the urlconnection via the bufferedreader
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line + "\n");
                }
                bufferedReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        return content.toString();
    }

    /**
     * Return a list of {@link String} objects (breed names) that has been built up from
     * parsing the given JSON response.
     */
    private static List<String> extractBreedNameFromJson(String responses) {

        // If the JSON string is empty or null, then return early.
        if (TextUtils.isEmpty(responses)) {
            return null;
        }

        // Create an empty ArrayList that we can start adding breed names to
        List<String> breedNameList = new ArrayList<>();


            try {
                JSONObject jsonObject = new JSONObject(responses);
                JSONObject messageObj = jsonObject.getJSONObject("message");

                for (Iterator<String> iter = messageObj.keys(); iter.hasNext(); ) {
                    String key = iter.next();
                    Log.w("key", key);
                    breedNameList.add(key);
                }

                Log.w("messageObj", messageObj.toString());

            } catch (JSONException e) {
                // If an error is thrown when executing any of the above statements in the "try" block,
                // catch the exception here, so the app doesn't crash. Print a log message
                // with the message from the exception.
                Log.e("QueryUtils", "Problem parsing the JSON results", e);
            }

        // Return the list of breeds
        return breedNameList;
    }

}

您的适配器类已经改变了一点:

package com.example.pbp22.dogbreed;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

public class CustomAdapter extends ArrayAdapter<Product> {

    List<Product> products;
    Context context;
    int resource;

    public CustomAdapter(Context context, int resource, List<Product> products) {
        super(context, resource, products);
        this.products = products;
        this.context = context;
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) getContext()
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_row, null, true);

        }
        Product product = getItem(position);

        ImageView image = (ImageView) convertView.findViewById(R.id.image);
        Picasso.with(context).load(product.getImage()).into(image);

        TextView breedName = (TextView) convertView.findViewById(R.id.breedName);
        breedName.setText(product.getBreedName());


        return convertView;
    }

}

这是你的主要活动:

    package com.example.pbp22.dogbreed;

import android.app.LoaderManager;
import android.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity{

    ListView list;

    LoaderManager.LoaderCallbacks<List<String>> breedNameLoaderCallbacks;
    LoaderManager.LoaderCallbacks<List<Breed>> breedLoaderCallbacks;

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

        list = (ListView) findViewById(R.id.list);

        breedLoaderCallbacks = new LoaderManager.LoaderCallbacks<List<Breed>>() {
            @Override
            public Loader<List<Breed>> onCreateLoader(int i, Bundle bundle) {

                List<String> breedNames = null;

                if (bundle != null) {
                    breedNames = new ArrayList<>(bundle.getStringArrayList("breed_name"));
                }

                return new BreedLoader(MainActivity.this, breedNames);
            }

            @Override
            public void onLoadFinished(Loader<List<Breed>> loader, List<Breed> products) {


                if (products != null && !products.isEmpty()) {
                    Log.v("DogXXX", products.toString());
                    CustomAdapter adapter = new CustomAdapter(
                            getApplicationContext(), R.layout.list_row, products
                    );
                    list.setAdapter(adapter);
                }
            }

            @Override
            public void onLoaderReset(Loader<List<Breed>> loader) {

            }
        };

        breedNameLoaderCallbacks = new LoaderManager.LoaderCallbacks<List<String>>() {
            @Override
            public Loader<List<String>> onCreateLoader(int i, Bundle bundle) {
                return new BreedNameLoader(MainActivity.this);
            }

            @Override
            public void onLoadFinished(Loader<List<String>> loader, List<String> strings) {

                Bundle breedNameBundle = new Bundle();
                breedNameBundle.putStringArrayList("breed_name", (ArrayList<String>) strings);
                getLoaderManager().initLoader(2, breedNameBundle, breedLoaderCallbacks);
            }

            @Override
            public void onLoaderReset(Loader<List<String>> loader) {

            }
        };

        getLoaderManager().initLoader(1, null, breedNameLoaderCallbacks);
    }
}

结果:

enter image description here

答案 2 :(得分:0)

尝试使用此代码轻松处理它..

将以下依赖项添加到应用程序级别的gradle文件中。

    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

然后在make改造对象类之后如下..

public class ApiClient {
private final static String BASE_URL = "https://dog.ceo/api/breed/";

public static ApiClient apiClient;
private Retrofit retrofit = null;
private Retrofit retrofit2 = null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}


private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}

}

然后创建api调用接口,如下面的代码..

public interface ApiInterface {
  @GET("{catname}/images")
  Call<Product> getProductData(@Path("catname") String breed);

}

然后在api中调用下面代码中的活动..

    public void getProductData() {
    ApiInterface apiInterface=ApiClient.getInstance().getClient().create(ApiInterface.class);
    Call<Product> productCall=apiInterface.getProductData("airedale"); // pass your string as dog brand.
    productCall.enqueue(new Callback<Product>() {
        @Override
        public void onResponse(Call<Product> call, retrofit2.Response<Product> response) {
            if (response.isSuccessful() && response.body()!=null){
                Product product=response.body();
                productlist.add(product);

                Log.d("Status",product.getStatus());
                Log.d("Message",product.getMessage().toString());
            }
        }

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

        }
    });
}

制作类似以下代码的产品类..

public class Product {

@SerializedName("status")
private String status;
@SerializedName("message")
private List<String> message = null;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public List<String> getMessage() {
    return message;
}

public void setMessage(List<String> message) {
    this.message = message;
}


@Override
public String toString() {
    return
            "Response{" +
                    "message = '" + message + '\'' +
                    ",status = '" + status + '\'' +
                    "}";
}

}