【问题标题】:Android HTTP client Throwing exceptionAndroid HTTP客户端抛出异常
【发布时间】:2013-11-25 01:05:59
【问题描述】:

我试图构建一个以 heroku 服务器作为后端的 android 应用程序。我正在尝试通过 heroku URL 将 JSON 数据作为字符串获取。

这是json字符串的链接

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

而url的内容是

[{"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"}}]

这是我的代码:

//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;

}

}

当我运行这段代码时,我得到的回报是

“大富翁?!”

所以我假设我遇到了麻烦

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

我从 2 天开始一​​直在修改代码,但我无法找出问题所在。请帮帮我。

谢谢!

【问题讨论】:

  • 请贴出logcat的输出,否则很难帮到你。
  • @CompuChip 你是对的。我错过了 INTERNET 许可。这解决了一切。谢谢!

标签: java android heroku httpclient


【解决方案1】:

请确保您已向INTERNET 授予您的应用程序权限,请参阅this SO question 了解如何执行此操作。

顺便说一句,我建议在您的代码中添加一些更具描述性的错误消息,例如查看e.getMessage()

【讨论】:

    【解决方案2】:

    首先网络操作应该异步完成,为此使用AsyncTask。其次,我建议使用 Log 函数进行错误处理。这里的工作代码(getJSON() 是输入函数):

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 2016-06-24
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      相关资源
      最近更新 更多