简体   繁体   中英

Android HTTP client Throwing exception

I was trying to build an android app with a heroku server as the backend. I am trying to get JSON data as string through a heroku URL.

Here's the link to the json string

http://le-wild-pesit.herokuapp.com/peeps/

and the contents of the url is

[{"peep":{"age":null,"created_at":"2013-11-23T08:58:09Z","id":1,"name":null,"updated_at":"2013-11-23T08:58:09Z"}},{"peep":{"age":null,"created_at":"2013-11-23T08:59:30Z","id":2,"name":null,"updated_at":"2013-11-23T08:59:30Z"}}]

Here's my code:

//imports go here
public class JSONParser 
{

public String getJSONFromUrl(String url)
{

    InputStream inpstr = null;
    String result = "";
    HttpResponse response = null;

    // HTTP
    try ,
    {           
        HttpClient httpclient = new DefaultHttpClient();
        try
        {
            response = httpclient.execute(new HttpGet(url));
            }
        catch(Exception e) 
        {
            return "dafuq?!";
        }
        HttpEntity entity = response.getEntity();
        inpstr = entity.getContent();
    } 
    catch(Exception e) 
    {
        return "SHIT!";
    }

    // Code to get string from input stream

 return result;

}

}

When I run this code, The return I get is

"dafuq?!"

So I'm assuming I have trouble with

response = httpclient.execute(new HttpGet(url));

I've been tinkering with the code from 2 days and I haven't been able to figure out what's wrong. Please help me out.

Thanks!

Please make sure you have given INTERNET permission to your application, see this SO question on how to do that.

As an aside note, I would recommend adding some more descriptive error messages to your code, look into e.getMessage() for example.

First of all network operation should be done asynchronously, use AsyncTask for this. Secondly I recommend to use Log function for error handling. And here working code( getJSON() is the enter function ):

public void getJSON() {
    String[] strings = new String[]{"http://le-wild-pesit.herokuapp.com/peeps/"};
    new AsyncJSONTask().execute(strings);
}


public JSONArray getJSONFromUrl(String url) {
    String resultString;
    HttpResponse response;
    JSONArray jsonArray = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        response = httpclient.execute(new HttpGet(url));
        HttpEntity entity = response.getEntity();
        resultString = EntityUtils.toString(entity);
        jsonArray = new JSONArray(resultString);
    } catch (Exception e) {
        Log.e("SOME_TAG", Log.getStackTraceString(e));
    }
    return jsonArray;
}

private class AsyncJSONTask extends AsyncTask<String, Void, JSONArray> {

    @Override
    protected JSONArray doInBackground(String... params) {
        String url = params[0];
        return getJSONFromUrl(url);
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {
        super.onPostExecute(jsonArray);
        //Here you do your work with JSON
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM