【问题标题】:Aquery ajax request seems not starting in androidAquery ajax 请求似乎没有在 android 中启动
【发布时间】:2016-04-10 16:56:10
【问题描述】:

我认为我为从服务器获取 JSON 数据而构建的代码没有启动。在 logcat 中没有来自“log.v”的文本。此外,我编写的代码似乎一直不适合 try-catch。我对 ajax 请求使用 A 查询。

其他人对此有疑问吗?

我想将 JSON 放入 hashmap 中,以便构建它的列表视图,但我无法将 JSON 中的数据获取到 logcat 中

问候

public class film_sound extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.activity_film_sound, container, false);
        Context context = inflater.getContext();
        String[] Film_Data =  getArguments().getStringArray("Film_Data");

        File ext = Environment.getExternalStorageDirectory();
        File cacheDir = new File(ext, "/android/data/be.moviechecker.programma/case");
        AQUtility.setCacheDir(cacheDir);
        final AQuery aq = new AQuery(context);

        /*Ophalen van de json*/
        String url = "http://app.be/data_2_0_0/sound.php?id=1";
        aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {
            @Override
            public void callback(String url, JSONObject json, AjaxStatus status) {
                if (json != null) {
                    JSONArray jArray = null;
                    try {
                        jArray = json.getJSONArray("soundtrack");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if (jArray.length() == 0) {
                        Log.v("debug", "dataleeg");
                    } else {
                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject json_data = null;
                            try {
                                json_data = jArray.getJSONObject(i);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            try {
                                Log.v("debug", json_data.getString("naam"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else {
                    //ajax error, show error code
                    Log.v("debug", aq.getContext() + "Error:" + status.getCode());
                }
            }
        });

        return view;
    }
}

【问题讨论】:

  • 您似乎使用了错误的网址。尝试在浏览器中打开您的网址(复制+粘贴)。没有返回json数据,只是找不到这个页面的错误。同时AQuery 正在尝试将返回的数据转换为 json,这就是为什么没有返回或记录任何内容的原因。尝试使用正确的 url 来获取 json 数据。
  • 这不是真正的 url,我从 php 代码中得到状态 200 和 json
  • 所以,然后发布您的真实 url,社区需要重现该问题,因此我们需要所有信息才能解决您的问题。
  • 我刚刚运行了你的代码,在 android logcat 中记录了错误,找不到 404。因此该代码可以完美运行。也许你只是忘了给AndroidManifest.xml添加互联网权限?
  • @rom4ek 我添加了正确的权限

标签: android jquery json ajax aquery


【解决方案1】:

在 phonegap 中,您必须在 config.xml

启用跨域请求
<access origin="http://domain.com/public/auth/app-login"/>

也许你可以看看这个:
WebView Javascript cross domain from a local HTML file

【讨论】:

    猜你喜欢
    • 2017-10-24
    • 1970-01-01
    • 2017-11-11
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2011-09-16
    相关资源
    最近更新 更多