Ryan Folz
Ryan Folz

Reputation: 43

Android: Getting JSON from a uri

I have looked in all of the past stack overflow questions, and I just can't seem to figure this out.

What I am wanting to do is give a uri and receive a JSON from it. Here is my code so far:

public void setUpTheJSONs() throws IOException, JSONException, URISyntaxException {
    jsonSummonerInfo = getJsonSummonerInfo();
    jsonSummonerRankedStats = getJsonRankedStats();
}

public JSONObject getJsonSummonerInfo() throws IOException, JSONException, URISyntaxException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/kinoscorpia?api_key=my_api_key_here");
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity entity = response.getEntity();

    if(entity != null){
        JSONObject temp =  new JSONObject(EntityUtils.toString(entity));
        return temp;
    } else {
        return null;
    }

}

}

However, I never get a response and the response = httpClient.execute(httpPost) line.

When you type the uri into a website, the JSON pops up, but not when the code runs.

Any help?

Thanks ☺

Upvotes: 0

Views: 1246

Answers (3)

JasperMoneyshot
JasperMoneyshot

Reputation: 357

Are you sure you're meaning to do a POST request? Your code doesn't seem to actually send anything to the server short of the url where in a POST request you would generally attach something to the requests entity.

Try sending it as a GET request and see if that solves your problem:

public JSONObject getJsonSummonerInfo() throws IOException, JSONException, URISyntaxException 
{
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/kinoscorpia?api_key=my_api_key_here");

    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();

    if(entity != null)
    {
        JSONObject temp =  new JSONObject(EntityUtils.toString(entity));
        return temp;
    } 
    else 
    {
        return null;
    }
}

Upvotes: 2

YukiNyaa
YukiNyaa

Reputation: 136

Code itself has no problems.
Perhaps the link is broken.
Check if the link throws http error or check code by this URI
https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=stackoverflow

Upvotes: 0

yanchenko
yanchenko

Reputation: 57206

With DroidParts:

public JSONObject getJsonSummonerInfo(Context ctx) throws HTTPException {
    return new RESTClient2(ctx).getJSONObject("https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/30170613/ranked?season=SEASON2015&api_key=(**MYAPIKEY**)");
}

Upvotes: 0

Related Questions