【问题标题】:How do I access the returned value from a Web API Method in Android?如何从 Android 中的 Web API 方法访问返回值?
【发布时间】:2014-05-14 03:21:37
【问题描述】:

在对如何做到这一点感到困惑之后(如 herehere 所示,我现在可以使用此代码成功连接到我的服务器应用程序和适当的 RESTful 方法:

public void onFetchBtnClicked(View v){
    if(v.getId() == R.id.FetchBtn){
        Toast.makeText(getApplicationContext(), "You mashed the button, dude.", Toast.LENGTH_SHORT).show();
    new CallAPI().execute("http://10.0.2.2:28642/api/Departments/GetCount?serialNum=4242");
    }
}

public static class CallAPI extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {

        String urlString=params[0]; // URL to call
        String resultToDisplay = "";
        InputStream in = null;

        // HTTP Get
        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedInputStream(urlConnection.getInputStream());
        } catch (Exception e ) {
            System.out.println(e.getMessage());
            return e.getMessage();
        }
        return resultToDisplay;

    }

    protected void onPostExecute(String result) {
        Log.i("FromOnPostExecute", result);
    }

} // end CallAPI

我意识到我需要为 resultToDisplay 分配一些东西(除了初始化时的空字符串),但是什么?我需要访问/转换为字符串的“in”的哪一部分?

更新

“手动”方式对我有用,但花哨的 apache io 实用程序“没那么多”(嗯,它编译...)。这是我的代码:

try {
    URL url = new URL(urlString);
    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
    in = new BufferedInputStream(urlConnection.getInputStream());
    resultToDisplay = getStringFromInputStream(in);
    total = IOUtils.toString(in);

resultToDisplay 的作业有效(我得到“18”)。总的分配没有(我得到,“”)。

注意:“getStringFromInputStream()”方法来自 Raghunandan 的链接。

更新 2

这很有效(使用 WIllJBD 的想法来使用 apache commons 的 IOUtils):

new CallWebAPI().execute("http://10.0.2.2:28642/api/Departments/GetCount?serialNum=4242");
. . .
private class CallWebAPI extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {

        String urlString=params[0]; // URL to call
        String result = "";

        // HTTP Get
        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection =  
                (HttpURLConnection)url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            if (null != inputStream)
                result= IOUtils.toString(inputStream);
        } catch (Exception e ) {
            System.out.println(e.getMessage());
            return e.getMessage();
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i("RenameTheWashingtonFootballTeamTheRedskinPeanuts", result);
    }
}

...所以显然没有必要在 build.gradle 的依赖项部分添加“编译文件('libs/commons-io-2.4.jar')”之类的东西,至少在有一次,根据this。如果有人可以验证不再需要 build.gradle 这样的 [m,pp]endments,我会很高兴。

更新 4

我刚刚注意到我无意中从 onPostExecute() 方法中删除了“@Override”,但它没有任何区别 - 没有它它可以正常工作,并且一旦我恢复它就可以正常工作。那么[不]拥有它有什么好处 - 它只是多余的绒毛吗?

【问题讨论】:

标签: java android android-asynctask http-get bufferedinputstream


【解决方案1】:

为什么不使用 IOUtils 之类的东西?

InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null)
    String content = IOUtils.toString(inputStream);

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html

现在您可以使用众多库之一将字符串解析为 Json 或 XML。

【讨论】:

  • 这对我不起作用;我会将详细信息放在更新中。
  • 我将我的答案更新为可能有效,很抱歉最初它对您不起作用。我可以保证它确实有效,因为我在我为 Android 制作的网络调用库中使用了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
相关资源
最近更新 更多