0

I'm having an issue with fetching a JSON from a URL with Android, and have the error stated in my titles with my URL. I have posted relevant code below. If more is required, I can provide it. At the moment, I have provided the snippets of code that are being flagged by the error.

The error:

    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

public class StatsActivity extends 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);

        }
    }
}
Nate
  • 392
  • 3
  • 5
  • 17
  • What's the error? stack trace, perhaps? – wns349 Apr 07 '14 at 02:10
  • Sorry, added the error into my main post. – Nate Apr 07 '14 at 02:14
  • your url [https://prod.api.pvp.net/api/lol/NA/v1.4/summoner/by-name/android.widget.EditText{41b713c0+VFED..CL+.F......+0,0-804,118+#7f08003d+app:id/name}?api_key=] does not look correct. Try encode the url parameters before making such HTTP requests. – D.S Apr 07 '14 at 02:18
  • I had figured the middle bit before the API key was scrambled as it was being brought in from another class. Is this not the case? Could it be its catching modifiers from the keyboard (shift and such)? – Nate Apr 07 '14 at 02:21
  • Did you try to debug it? The sequence is definitely wrong for your program. encodedUrl is NOT being initialized, which means somehow onPreExecute is not being called or some exception while encoding onPreExecute() method is preventing url encoding. Also I would not encode the whole URL, just the parameter values. – D.S Apr 07 '14 at 02:31
  • Doing it that way (encoding the parameter values only), still leaves this for the name being passed: android.widget.EditText%7B41b6cbc8+VFED..CL+.F......+0%2C0-804%2C118+%237f08003d+app%3Aid%2Fname%7D The error has changed, however, to this: Illegal character in query at index 166. This index is the "." in android.widget... – Nate Apr 07 '14 at 02:49

0 Answers0