0

oooooops:|Android volley requestString返回null onResponse

为什么NULL?

服务器端正在修复。

所有方法都修复。

为什么NULL?类似的代码工作,但这个代码不工作。全部logs是确定的。

fragmet对话框

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

但在这个params为工作:

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

在输出日志是真实的:

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

为什么NULL ???? ?

回答

3
use this 
String crappyPrefix = "null"; 

if(result.startsWith(crappyPrefix)){ 
    result = result.substring(crappyPrefix.length(), result.length()); 
} 
JSONObject jo = new JSONObject(result); 
+0

这不是问题。服务器正确的杰森代码和修复。我迁移到“改造”。 f * ck凌空。感谢评论。 – grizzly

+0

如果这有助于解决您的问题,请将其标记为已接受。 –

-1
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) { 
     } 
    } 
+0

我尝试'trustAllCertificates()',但没有奏效。返回null – grizzly

+0

logcat check logcat中显示的任何错误消息。 –

+0

'org.json.JSONException:类型org.json.JSONObject $ 1的值为null无法转换为JSONArray'警告logcat – grizzly