【问题标题】:Android volley requestString return null onResponseAndroid volley requestString 返回 null onResponse
【发布时间】:2017-02-25 18:47:38
【问题描述】:

呜呜呜:|

为什么是 NULL?

服务器端已修复。

所有方法都修复了。

为什么是NULL?类似的代码是有效的,但此代码无效。所有logs 都可以。

片段对话框中:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.player_artists_dialog, container, false);

    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    getDialog().setCancelable(true);

    model = this.getArguments().getParcelable("model");

    progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
    listView = (ListView) rootView.findViewById(R.id.listView);

    listView.setVisibility(View.INVISIBLE);
    progressBar.setVisibility(View.VISIBLE);

    StringRequest request = new StringRequest(Request.Method.POST,
            ".. url ..",

            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    Toast.makeText(getContext(), response, Toast.LENGTH_LONG).show(); // show null

                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError ex) {
                }
            }) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("a", String.valueOf(model.getTr()));
            params.put("b", String.valueOf(model.getCat()));
            return params;
        }

    };

    RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();
    queue.add(request);

    return rootView;
}

但是在这个参数上是有效的:

@Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("a", "104");
            params.put("b", "1");
            return params;
        }

输出 LOG 为真:

String.valueOf(model.getTr()); // 104 is true
String.valueOf(model.getCat()); // 1 is true

为什么是 NULL?????

【问题讨论】:

    标签: android android-fragments android-volley android-networking


    【解决方案1】:
    use this
    String crappyPrefix = "null";
    
    if(result.startsWith(crappyPrefix)){
        result = result.substring(crappyPrefix.length(), result.length());
    }
    JSONObject jo = new JSONObject(result);
    

    【讨论】:

    • 这不是问题。服务器正确的 jason 代码并修复。我迁移到retrofit。他妈的凌空抽射。感谢您的评论。
    【解决方案2】:
    Use to import self-signed SSL certificate to Volley on Android 4.1+
    It will make volley to Trust all SSL certificates
    I hope it will work.
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.player_artists_dialog, container, false);
    
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        getDialog().setCancelable(true);
    
        model = this.getArguments().getParcelable("model");
    
        progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
        listView = (ListView) rootView.findViewById(R.id.listView);
    
        listView.setVisibility(View.INVISIBLE);
        progressBar.setVisibility(View.VISIBLE);
    
            //use trustAllCertificates here.
       trustAllCertificates();
    
    
        StringRequest request = new StringRequest(Request.Method.POST,
                ".. url ..",
    
                new Response.Listener<String>() {
    
                    @Override
                    public void onResponse(String response) {
    
                        Toast.makeText(getContext(), response, Toast.LENGTH_LONG).show(); // show null
    
                    }
                },
    
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError ex) {
                    }
                }) {
    
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("a", String.valueOf(model.getTr()));
                params.put("b", String.valueOf(model.getCat()));
                return params;
            }
    
        };
    
        RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();
        queue.add(request);
    
        return rootView;
    }
     private void trustAllCertificates() {
            try {
                TrustManager[] trustAllCerts = new TrustManager[] {
                        new X509TrustManager() {
                            public X509Certificate[] getAcceptedIssuers() {
                                X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                                return myTrustedAnchors;
                            }
    
                            @Override
                            public void checkClientTrusted(X509Certificate[] certs, String authType) {}
    
                            @Override
                            public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                        }
                };
    
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String arg0, SSLSession arg1) {
                        return true;
                    }
                });
            } catch (Exception e) {
            }
        }
    

    【讨论】:

    • 我尝试trustAllCertificates() 但没有成功。返回空
    • org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray 警告日志猫
    • 这是因为此结果不是有效的 JSON 内容。如果您确实从服务器收到此无效内容,解决方法可以是在解析 JSON 之前删除 null。
    • String crappyPrefix = "null"; if(result.startsWith(crappyPrefix)){ result = result.substring(crappyPrefix.length(), result.length()); } JSONObject jo = new JSONObject(result);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多