LoopJ AndroidAsyncHttp - 参数 - 冲突



我想从"Open汇率"中导入"latest.jason"以获取最新的货币汇率。

但是当我编写"AsyncHttpClient"时,它会创建以下"未实现的类":

@Override
        public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
            // TODO Auto-generated method stub
        }

但我想要这个 - (运行)

            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 " 作为参数...

但我想——" 字符串 arg2 "

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

最适合处理 JSON 数据是在 JsonHttpResponseHandler 中

试试这个...

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

相关内容

  • 没有找到相关文章

最新更新