2017-03-15 82 views
1

我開發了一個android應用程序,它將傳感器數據發送到燈服務器,用於在後臺使用服務發送數據,並且支持我使用php。當我運行應用程序後臺服務啓動時,我正從服務器獲取數據成功插入消息,燈服務器行正在更新,但值不存儲。空值存儲請幫我找出代碼中的問題在哪裏?數據沒有正確接收?

public void data() throws JSONException { 
final String LIGHTSENS, LIGHTDATA, TEMPSENSOR, TEMPDATA, AMBTEMPSENSOR, AMBTEMDATA, PRESSURESENS, PRESSUREDATA, LATTITUDE, LONGITUDE, DATE, TIME; 
     LIGHTSENS = String.valueOf(lr1); 
     LIGHTDATA = String.valueOf(ld1); 
     TEMPSENSOR = String.valueOf(tr1); 
     TEMPDATA = String.valueOf(td1); 
     AMBTEMPSENSOR = String.valueOf(atr1); 
     AMBTEMDATA = String.valueOf(atd1); 

     PRESSURESENS = String.valueOf(pr1); 
     PRESSUREDATA = String.valueOf(pd1); 
     LATTITUDE = String.valueOf(la); 
     LONGITUDE = String.valueOf(lg); 


StringRequest stringRequest = new StringRequest(Request.Method.POST, Server_url, 
       new Response.Listener<String>() 
       { 
        @Override 
        public void onResponse(String response) 
        { 
         Toast.makeText(sens.this, response.toString(), Toast.LENGTH_SHORT).show(); 


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

       Toast.makeText(sens.this, error.toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }) { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError, AuthFailureError { 
       HashMap<String, String> params = new HashMap<String, String>(); 
       params.put("LIGHTSENS", LIGHTSENS); 
       params.put("LIGHTDATA", LIGHTDATA); 
       params.put("TEMPSENSOR", TEMPSENSOR); 
       params.put("TEMPDATA", TEMPDATA); 
       params.put("AMBTEMPSENSOR", AMBTEMPSENSOR); 
       params.put("AMBTEMDATA", AMBTEMDATA); 
       params.put("PRESSURESENS", PRESSURESENS); 
       params.put("PRESSUREDATA", PRESSUREDATA); 
       params.put("LATTITUDE", LATTITUDE); 
       params.put("LONGITUDE", LONGITUDE); 


       return super.getParams(); 
       //params.put("Content-Type", "application/json; charset=utf-8"); 
       //params.put("User-agent", System.getProperty("http.agent")); 

       //return params; 

      } 

     }; 

     //Mysingleton.getmInstance(sens.this).addTorequestque(stringRequest); 


     // RequestQueue rq = Volley.newRequestQueue(this); 
     RequestQueue requestQueue= Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 

     // RequestQueue requestQueue = Volley.newRequestQueue(this); 
     //requestQueue.add(stringRequest); 
    } 
+0

爲什麼你返回super.getParams(); –

+0

即使我把return.params;我無法收回我嘗試過的那種。你可以看到我評論該代碼行。 – bhavya

+0

請先調試您的服務以確保您收到空...您的服務有可能會在插入數據時出現問題。 –

回答

0

試試這個,

private void data() { 

     final String LIGHTSENS, LIGHTDATA, TEMPSENSOR, TEMPDATA, AMBTEMPSENSOR, AMBTEMDATA, PRESSURESENS, PRESSUREDATA, LATTITUDE, LONGITUDE, DATE, TIME; 
     LIGHTSENS = String.valueOf(lr1); 
     LIGHTDATA = String.valueOf(ld1); 
     TEMPSENSOR = String.valueOf(tr1); 
     TEMPDATA = String.valueOf(td1); 
     AMBTEMPSENSOR = String.valueOf(atr1); 
     AMBTEMDATA = String.valueOf(atd1); 

     PRESSURESENS = String.valueOf(pr1); 
     PRESSUREDATA = String.valueOf(pd1); 
     LATTITUDE = String.valueOf(la); 
     LONGITUDE = String.valueOf(lg); 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, Server_url, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Toast.makeText(sens.this, response.toString(), Toast.LENGTH_SHORT).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(sens.this, error.toString(), Toast.LENGTH_SHORT).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<>(); 
       params.put("LIGHTSENS", LIGHTSENS); 
       params.put("LIGHTDATA", LIGHTDATA); 
       params.put("TEMPSENSOR", TEMPSENSOR); 
       params.put("TEMPDATA", TEMPDATA); 
       params.put("AMBTEMPSENSOR", AMBTEMPSENSOR); 
       params.put("AMBTEMDATA", AMBTEMDATA); 
       params.put("PRESSURESENS", PRESSURESENS); 
       params.put("PRESSUREDATA", PRESSUREDATA); 
       params.put("LATTITUDE", LATTITUDE); 
       params.put("LONGITUDE", LONGITUDE); 
       Log.d(TAG, "@@@ params:" + params + " URL:" + Server_url); 
       return params; 
      } 
     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(sens.this); 
     requestQueue.add(stringRequest); 
    }