我正在使用异步任务从api服务器获取信息并更新UI。但是我在代码的这一行得到了一个错误:
public Drawable[] getSummonerSpells(int game) throws JSONException, IOException {
Drawable drawable[] = new Drawable[] {null, null};
Bitmap bd = null;
int spell1 = 0, spell2 = 0;
if(jsonSummonerRecentGames!=null){
JSONArray array = jsonSummonerRecentGames.getJSONArray("games");
if((game) <= array.length()) {
JSONObject object = array.getJSONObject(game - 1);
if(object.has("spell1")) {
spell1 = object.getInt("spell1");
}
if(object.has("spell2")){
spell2 = object.getInt("spell2");
}
}
}
StringBuilder url = new StringBuilder("https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/summoner-spell/" + spell1 + "?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2");
HttpGet get = new HttpGet(url.toString());
HttpResponse r = httpClient.execute(get); *********ERROR HERE*********
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject jsonObject = new JSONObject(data);
String key = jsonObject.getString("key");
try {
URL url2 = new URL("http://ddragon.leagueoflegends.com/cdn/5.2.1/img/spell/" + key + ".png");
InputStream is = new BufferedInputStream(url2.openStream());
bd = BitmapFactory.decodeStream(is);
} catch(Exception e2){}
if(bd != null){
drawable[0] = new BitmapDrawable(bd);
}
url = new StringBuilder("https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/summoner-spell/" + spell2 + "?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2");
get = new HttpGet(url.toString());
r = httpClient.execute(get);
e = r.getEntity();
data = EntityUtils.toString(e);
jsonObject = new JSONObject(data);
key = jsonObject.getString("key");
try {
URL url2 = new URL("http://ddragon.leagueoflegends.com/cdn/5.2.1/img/spell/" + key + ".png");
InputStream is = new BufferedInputStream(url2.openStream());
bd = BitmapFactory.decodeStream(is);
} catch(Exception e2){}
if(bd != null){
drawable[1] = new BitmapDrawable(bd);
}
return drawable;
}
当我调试它时,输入的URL是:
此网站提供了JSON回复。
我在doInBackground()方法中为此异步任务调用此方法:
Drawable[] summonerSpell = null;
try {
summonerSpell = data.getSummonerSpells(2);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
从我的第一个异步任务调用异步任务:
protected void onPostExecute(Long aLong) {
super.onPostExecute(aLong);
new BackgroundStuffGameOne().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataClass);
new BackgroundStuffGameTwo().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataClass);
}
有关为何调用此错误的任何想法?谢谢:))
答案 0 :(得分:0)
从问题中的代码看,httpClient
似乎没有初始化。
如果你已经声明了它(你好像已经这样做了),你可以在使用之前初始化它:
httpClient = new DefaultHttpClient(); //Added
StringBuilder url = new StringBuilder("https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/summoner-spell/" + spell1 + "?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2");
HttpGet get = new HttpGet(url.toString());
HttpResponse r = httpClient.execute(get); //Should work now
另外,请注意,请确保您在AndroidManifest.xml中拥有INTERNET
权限:
<uses-permission android:name="android.permission.INTERNET" />
还有一点需要注意,DefaultHttpClient
has been deprecated。
有关使用HTTPUrlConnection
的良好指南,请参阅here,这是一个很好的替代方案。