PokeAPI 没有响应
Posted
技术标签:
【中文标题】PokeAPI 没有响应【英文标题】:No response from PokeAPI 【发布时间】:2020-09-29 17:07:07 【问题描述】:我试图在我的 Pokedex 应用程序中从 Pokeapi 获取 JSONObject,但似乎没有响应并且产生了凌空错误。但是,该链接在显示 JSON 的浏览器中打开得非常好。我尝试了指向 JSON 的不同链接,但它仍然不起作用。 androidManifest 添加了使用互联网的权限。请求被添加到 RequestQueue。问题可能出在哪里?
这是我的 PokedexAdapter 类:
package com.example.pokedex;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class PokedexAdapter extends RecyclerView.Adapter<PokedexAdapter.PokedexViewHolder>
public static class PokedexViewHolder extends RecyclerView.ViewHolder
public LinearLayout containerView;
public TextView textView;
PokedexViewHolder(View view)
super(view);
containerView = view.findViewById(R.id.pokedex_row);
textView = view.findViewById(R.id.pokedex_row_text_view);
containerView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Pokemon current = (Pokemon) containerView.getTag();
Intent intent = new Intent(v.getContext(), PokemonActivity.class);
intent.putExtra("name", current.getName());
//intent.putExtra("number", current.getNumber());
v.getContext().startActivity(intent);
);
private List<Pokemon> pokemon = new ArrayList<>();
private RequestQueue requestQueue;
public PokedexAdapter(Context context)
requestQueue = Volley.newRequestQueue(context);
loadPokemon();
public void loadPokemon()
String url = "https://pokeapi.co/api/v2/pokemon?limit=151";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
@Override
public void onResponse(JSONObject response)
try
JSONArray results = response.getJSONArray("results");
for (int i = 0; i < results.length(); i++)
JSONObject result = results.getJSONObject(i);
pokemon.add(new Pokemon(
result.getString("name"),
result.getString("url")));
notifyDataSetChanged();
catch (JSONException e)
Log.e("cs50", "onResponse: ", e);
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
Log.d("cs50", "onErrorResponse: pokemon list error");
);
requestQueue.add(request);
@NonNull
@Override
public PokedexViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.pokedex_row, parent, false);
return new PokedexViewHolder(view);
@Override
public void onBindViewHolder(@NonNull PokedexViewHolder holder, int position)
Pokemon current = pokemon.get(position);
holder.textView.setText(current.getName());
holder.containerView.setTag(current);
@Override
public int getItemCount()
return pokemon.size();
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pokedex">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".PokemonActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
结果 - 屏幕上没有显示任何内容并产生此错误:
2020-06-09 19:09:37.469 15137-15137/com.example.pokedex D/cs50:onErrorResponse:口袋妖怪列表错误
【问题讨论】:
【参考方案1】:通过重新安装应用程序解决了问题。
这个帖子很有帮助: java.net.SocketException: socket failed: EPERM (Operation not permitted)
【讨论】:
以上是关于PokeAPI 没有响应的主要内容,如果未能解决你的问题,请参考以下文章
请问linux 系统文件夹 没有响应可能是啥原因,用 cd 命令没有响应,ls -l 也没响应,