我无法使用Volley库将数据发布到Mysql数据库

时间:2017-07-06 14:14:33

标签: android android-studio android-volley

以下是抛出以下异常的凌空代码 "尝试调用虚方法' int java.lang.String.hashCode()"。

String url = "http://192.168.60.02/TransportTracking/main.php";

StringRequest postRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response) {
                // response
                Log.d("Response", response);
            }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                // error
                Log.d("Error.Response", error.getMessage());
            }
        }
) {
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String>  params = new HashMap<String, String>();
        params.put("long","100");
        params.put("lat", "200");
        params.put("recordtime","acbcbcb");

        return params;
    }
};
    AppController.getInstance().addToRequestQueue(postRequest, "tag_json_obj");

以下是错误日志:

07-06 08:07:54.315 2837-2837 / com.example.jawadtariq.uogtransporttrackingsystem E / AndroidRuntime:FATAL EXCEPTION:main                                                                                                  处理:com.example.jawadtariq.uogtransporttrackingsystem,PID:2837

  

java.lang.NullPointerException:尝试调用虚方法&#39; int   java.lang.String.hashCode()&#39;在null对象引用上                                                                                                        在com.android.volley.Request。(Request.java:136)                                                                                                        在   com.android.volley.toolbox.StringRequest。(StringRequest.java:43)

                                                                                                 at com.example.jawadtariq.uogtransporttrackingsystem.LocationService$3.<init>(LocationService.java:98)
                                                                                                 at com.example.jawadtariq.uogtransporttrackingsystem.LocationService.onConnected(LocationService.java:81)
                                                                                                 at com.google.android.gms.common.internal.zzac.zzn(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzbco.zzm(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzbcc.zzpY(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzbcc.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzbcw.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.internal.zzbbh.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzn.zzrj(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zze.zzs(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source)
                                                                                                 at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最后要提到的是,我在发布此处之前已经使用Google搜索此异常,并且大多数问题都有错误的网址。但就我而言,网址是正确的。

2 个答案:

答案 0 :(得分:1)

声明一个volley request-queue的实例,然后在其中添加一个stringRequest。 喜欢

RequestQueue requestQueue =  
             Volley.newRequestQueue(getApplicationContext());
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response) {
                // response
                Log.d("Response", response);
        }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                // error
                Log.d("Error.Response", error.getMessage());
            }
        }
    ){
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String>  params = new HashMap<String, String>();
        params.put("long","100");
        params.put("lat", "200");
        params.put("recordtime","acbcbcb");
        return params;
    }
};

requestQueue.add(postRequest);

这是我的工作代码。

答案 1 :(得分:0)

你也可以在这里了解更多关于排球的信息 http://www.androidhive.info/2014/05/android-working-with-volley-library-1/