首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LoopJ AndroidAsyncHttp -参数-冲突

LoopJ AndroidAsyncHttp -参数-冲突
EN

Stack Overflow用户
提问于 2014-11-28 12:30:51
回答 2查看 189关注 0票数 1

我想从"OpenExchange rates“中导入"latest.jason”,以获得最新的货币汇率。

但是当我写"AsyncHttpClient“时,它创建了下面的”未实现的类“:

代码语言:javascript
复制
@Override
        public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
            // TODO Auto-generated method stub

        }

但我想要这个-(运行)

代码语言:javascript
复制
            public void onSuccess(String arg2) {
            Log.i("MYFIRSTAPP" , "HTTP Successs");
            try {
                JSONObject jsonObj = new JSONObject(arg2);
                JSONObject ratesObject = jsonObj.getJSONObject("rates");
                Double gbpRate = ratesObject.getDouble("GBP");
                Double eurRate = ratesObject.getDouble("EUR");
                Log.i("MYFIRSTAPP", "GBP" +gbpRate);
                Log.i("MYFIRSTAPP", "EUR" +eurRate);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

我得到的问题是: onSuccess使用“int arg0,Header[] arg1,byte[] arg2”作为参数……

但我想要-“String arg2”

EN

回答 2

Stack Overflow用户

发布于 2014-11-28 12:52:50

试试这个。

代码语言:javascript
复制
AsyncHttpClient client = new AsyncHttpClient();
    client.get(URL, new AsyncHttpResponseHandler()
    {
        @Override
        public void onFailure(int statusCode, Header[] header, byte[] content,
                Throwable error)
        {
            // show error messages here
           super.onFailure(statusCode, error, content);
        }

        @Override
        public void onSuccess(int statusCode, Header[] header, byte[] content)
        {
            if (statusCode == 200)
            {
                try
                {
                    //convert byte[] to string.
                    String contentStr = content.toString();

                    Log.i("Tag", "content" + URL + " " + contentStr );
                    
                    //String to JSONObject.
                    JSONObject jsonObj = new JSONObject(contentStr );
                    JSONObject ratesObject = jsonObj.getJSONObject("rates");
                    Double gbpRate = ratesObject.getDouble("GBP");
                    Double eurRate = ratesObject.getDouble("EUR");
                    Log.i("MYFIRSTAPP", "GBP" + gbpRate);
                    Log.i("MYFIRSTAPP", "EUR" + eurRate);
                }
                catch (Exception e)
                {
                    e.toString();
                }
            }
            else
            {
                // show network error toast here;
            }
        }
    });
票数 0
EN

Stack Overflow用户

发布于 2014-11-28 15:38:57

AsyncHttpResponseHandler及其子类中有许多onSuccessonFailure方法的变体。

最适合处理JSON数据的是JsonHttpResponseHandler

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27181928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档