Volley库NetworkDispatcher.run错误

时间:2016-03-12 14:14:45

标签: android android-volley

在我的程序中有NetworkDispatcher.run错误我正在使用volley库。 我的程序部分是。我已经完成但没有任何工作请帮助我做我的程序。我通过许多网络内容搜索但没有任何帮助我。我正在做的是在Android中的登录验证程序。

if(!(usr.getText().toString().length()<11)&&!(pwd.getText().toString().length()<3)) {
                    susr = usr.getText().toString();
                    spwd = pwd.getText().toString();
                    String whois = susr.substring(0, Math.min(susr.length(), 2));
                    if (whois.equals("AG") || whois.equals("RT")) {
                        if (whois.equals("AG")) {

                            id = susr.substring(susr.length() - 9);
                            Toast.makeText(getApplicationContext(), id, Toast.LENGTH_LONG).show();
                            StringRequest stringRequest = new StringRequest(Request.Method.POST, agenturl,
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String response) {
                                            //Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                                            if(response.equals("0")){
                                                Toast.makeText(getApplicationContext(),"Login successful",Toast.LENGTH_LONG).show();
                                                Intent intent=new Intent(getBaseContext(),Deals.class);
                                                startActivity(intent);
                                            }else{
                                                Toast.makeText(getApplicationContext(),"username or password is incorrect",Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
                                            //Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_LONG).show();
                                            if(error instanceof NoConnectionError) {
                                                Toast.makeText(getApplicationContext(),"No internet Access, Check your internet connection.", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    }){
                                @Override
                                protected Map<String,String> getParams(){
                                    Map<String,String> params = new HashMap<String, String>();
                                    params.put("id",id);
                                    params.put("pwd",spwd);
                                    return params;
                                }

                            };
                            requestQueue.add(stringRequest);
                        }
                        else if(whois.equals("RT")){

                            StringRequest stringRequest2 = new StringRequest(Request.Method.POST, retailerurl,
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String response) {
                                            //Toast.makeText(getApplicationContext(),"hf",Toast.LENGTH_LONG).show();
                                            //Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
                                            if(response.equals("0")){
                                                Toast.makeText(getApplicationContext(),"Login successful",Toast.LENGTH_LONG).show();
                                                Intent intent=new Intent(getBaseContext(),Deals.class);
                                                startActivity(intent);
                                            }else{
                                                Toast.makeText(getApplicationContext(),"username or password is incorrect",Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
                                            //Toast.makeText(MainActivity.this,error.toString(), Toast.LENGTH_LONG).show();
                                            if(error instanceof NoConnectionError) {
                                                Toast.makeText(getApplicationContext(),"No internet Access, Check your internet connection.", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    }){
                                @Override
                                protected Map<String,String> getParams(){
                                    Map<String,String> params = new HashMap<String, String>();
                                    params.put("id",id);
                                    params.put("pwd",spwd);
                                    return params;
                                }

                            };
                            requestQueue2.add(stringRequest2);
                        }
                    } else{
                        Toast.makeText(getApplicationContext(), "The user id or password is incorrect", Toast.LENGTH_LONG).show();
                        usr.setText("");
                    }
                }else {
                    Toast.makeText(getApplicationContext(), "The user id or password is incorrect", Toast.LENGTH_LONG).show();
                    usr.setText("");
                }

和错误日志cat是。

E/Volley: [197] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                               at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                               at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                               at com.android.volley.Request.encodeParameters(Request.java:484)
                                                               at com.android.volley.Request.getBody(Request.java:470)
                                                               at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253)
                                                               at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)
                                                               at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
                                                               at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
                                                               at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

3 个答案:

答案 0 :(得分:9)

错误是说你得到NullPointerException。

  

这意味着您在此代码中使用的变量在运行时为空。

答案 1 :(得分:2)

我明白了。 else if(whois.equals("RT")){之后我忘了放id = susr.substring(susr.length() - 9);

  

由于null值,主要发生 NetworkDispatcher.run错误   传入了凌空库的getParams()方法。所以我们必须意识到   关于将值传递给getParams()方法。

答案 2 :(得分:2)

此错误经常发生在服务器上发送数据 错误

尝试在此代码中放入硬编码值,以确保没有任何服务器 错误

           public Map<String, String> getParams()
              {
                Map<String, String> params = new HashMap<String, String>();
                params.put("rollno","1234");
                params.put("password","abcd");
                return params;
                  }
                };

如果它正常工作,那么你应该调试以确保此参数获得值

             params.put("id",id);
              params.put("pwd",spwd);