【问题标题】:LoopJ AndroidAsyncHttp - Parameters - ConflictLoopJ AndroidAsyncHttp - 参数 - 冲突
【发布时间】:2015-01-26 17:10:41
【问题描述】:

我想从“OpenExchange 汇率”中导入“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”

【问题讨论】:

  • 能否提供更多代码或完整代码?
  • 你实现了哪个接口?

标签: android eclipse android-async-http


【解决方案1】:

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

最适合处理JSON数据的是JsonHttpResponseHandler

【讨论】:

  • AsyncHttpResponseHandler 中只有一种 onSuccess() 变体和一种 onFailure() 变体。
  • 同意。现在,在 1.4.9 版本中(在 AsyncHttpResponseHandler 中)只有onSuccessonFailure 的一种变体
【解决方案2】:

试试这个...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多