我在使用Android从网址获取JSON时出现问题,并且我的网址中的标题中包含错误。我在下面发布了相关代码。如果需要更多,我可以提供。目前,我提供了由错误标记的代码片段。
错误:
04-06 23:13:44.533 20566-20855/edu.appdesign.leaguestats E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: edu.appdesign.leaguestats, PID: 20566
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 166: https://prod.api.pvp.net/api/lol/NA/v1.4/summoner/by-name/android.widget.EditText%7B41b76038+VFED..CL+.F......+0%2C0-804%2C118+%237f08003d+app%3Aid%2Fname%7D?api_key=<d96236d2-6ee3-4cfd-afa7-f41bdbc11128>
at java.net.URI.create(URI.java:727)
at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java)
at edu.appdesign.leaguestats.JSONParser.getJSONFromUrl(JSONParser.java:45)
at edu.appdesign.leaguestats.StatsActivity$GetStats.doInBackground(StatsActivity.java:63)
at edu.appdesign.leaguestats.StatsActivity$GetStats.doInBackground(StatsActivity.java:24)
at android.os.AsyncTask$2.call(AsyncTask.java)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
04-06 23:13:44.543 19569-20025/? E/ActivityManager﹕ App crashed! Process: edu.appdesign.leaguestats
公共类StatsActivity扩展了Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats_activity);
GetStats stats = new GetStats();
stats.execute();
}
class GetStats extends AsyncTask<Void, Void, Void> {
private String api_key="d96236d2-6ee3-4cfd-afa7-f41bdbc11128";
String region = MainActivity.region;
String name = MainActivity.name;
String url = null;
String encodedName = null;
String encodedKey = null;
String encodedRegion = null;
TextView textId = (TextView) findViewById(R.id.id);
TextView textName = (TextView) findViewById(R.id.name);
TextView textProfileIconId = (TextView) findViewById(R.id.profileIconId);
TextView textRevisionDate = (TextView) findViewById(R.id.revisionDate);
TextView textSummonerLevel = (TextView) findViewById(R.id.summonerLevel);
long jId;
String jName;
int jProfileIconId;
long jRevisionDate;
long jSummonerLevel;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
encodedName = URLEncoder.encode(name, "UTF-8");
encodedKey = URLEncoder.encode(api_key, "UTF-8");
encodedRegion = URLEncoder.encode(region, "UTF-8");
url = "https://prod.api.pvp.net/api/lol/" + encodedRegion + "/v1.4/summoner/by-name/" + encodedName + "?api_key=<" + encodedKey + ">";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... arg0) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
jId = json.getLong("id");
jName = json.getString("dataVersion");
jProfileIconId = json.getInt("profileIconId");
jRevisionDate = json.getLong("revisionDate");
jSummonerLevel = json.getLong("summonerLevel");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
/**
* Put json data on screen
* */
String id = String.valueOf(jId);
String name = String.valueOf(jName);
String icon = String.valueOf(jProfileIconId);
String rev = String.valueOf(jRevisionDate);
String summLevel = String.valueOf(jSummonerLevel);
textId.setText(id);
textName.setText(name);
textProfileIconId.setText(icon);
textRevisionDate.setText(rev);
textSummonerLevel.setText(summLevel);
}
}
}