【问题标题】:Executing an API call in Android在 Android 中执行 API 调用
【发布时间】:2012-11-27 23:10:53
【问题描述】:

我正在尝试对地址进行 API 调用:apps.smilemachine.com/smilefactory/api/v1.0/speedup。我只想执行 url(上图),以便它增加机器并更改存储在 Smilemachine.com 的速度值?没什么特别的,我只需要像在任何浏览器的地址栏中输入一样执行 url?

有没有人对此或代码 sn-ps 有任何建议?

我当前的代码只是打开浏览器,请在下面找到:

increasespeed.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        Intent increasespeed = new Intent(null, Uri.parse("http://apps.smilemachine.com/smilefactory/api/v1.0/speedup"));
                startActivity(increasespeed);

    }
});

【问题讨论】:

  • 感谢 Rawkode 的提醒

标签: android android-intent android-emulator android-widget


【解决方案1】:

只需通过 HTTP 连接获取它:

URL url = new URL("http://apps.smilemachine.com/smilefactory/api/v1.0/speedup");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

【讨论】:

  • 谢谢 PearsonArtPhoto,你知道实现同样事情的另一种方法吗,这似乎需要无穷无尽的 try catch 语句?
  • 无休止的 try catch 语句?如果您对捕获准确的异常不那么大惊小怪,请使用 try { .... } catch (Exception e) { // Log this }
猜你喜欢
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2021-02-17
相关资源
最近更新 更多