2014-07-18 69 views
2

在研究問題時遇到了(Returned JSONObject is always null)此問題並將接受的答案應用於我的代碼。當我連續兩次測試它時,它確實有效,但在早上醒來測試最後一次移動,它又開始返回null。JSONObject返回時爲空

PHP:

<?php 
require_once("C:\\inetpub\\vhosts\\tablet-secure.php"); 
require_once("_functions.php"); 
$db=sqlsrv_connect($DB_HOST,$DB_CONNECTION); 

if($db) 
{ 

    if (!empty($_POST['orders_certs_gas_svc_rec_appliance_id']) && !empty($_POST['orders_certs_gas_svc_rec_appt_id']) && 
     !empty($_POST['orders_certs_gas_svc_rec_eng_sig']) && !empty($_POST['orders_certs_gas_svc_rec_date'])) 
    { 
     $orders_certs_gas_svc_rec_fl_atms_fan_assst_fan_diltn_chk = $_POST['orders_certs_gas_svc_rec_fl_atms_fan_assst_fan_diltn_chk']; 
     $orders_certs_gas_svc_rec_vent_sz_h_l_chk = $_POST['orders_certs_gas_svc_rec_vent_sz_h_l_chk']; 
     $orders_certs_gas_svc_rec_wat_fuel_snd_chk = $_POST['orders_certs_gas_svc_rec_wat_fuel_snd_chk']; 
     $orders_certs_gas_svc_rec_elec_fus_chk = $_POST['orders_certs_gas_svc_rec_elec_fus_chk']; 
     $orders_certs_gas_svc_rec_correct_valv_arrng_chk = $_POST['orders_certs_gas_svc_rec_correct_valv_arrng_chk']; 
     $orders_certs_gas_svc_rec_isol_avail_elec_fuel_1met_chk = $_POST['orders_certs_gas_svc_rec_isol_avail_elec_fuel_1met_chk']; 
     $orders_certs_gas_svc_rec_boilr_plntrm_cln_clr_chk = $_POST['orders_certs_gas_svc_rec_boilr_plntrm_cln_clr_chk']; 
     $orders_certs_gas_svc_rec_heat_exch_chk = $_POST['orders_certs_gas_svc_rec_heat_exch_chk']; 
     $orders_certs_gas_svc_rec_ignit_chk = $_POST['orders_certs_gas_svc_rec_ignit_chk'];    
     $orders_certs_gas_svc_rec_gas_vlv_chk = $_POST['orders_certs_gas_svc_rec_gas_vlv_chk'];    
     $orders_certs_gas_svc_rec_fan_chk = $_POST['orders_certs_gas_svc_rec_fan_chk'];    
     $orders_certs_gas_svc_rec_safety_dev_chk = $_POST['orders_certs_gas_svc_rec_safety_dev_chk'];   
     $orders_certs_gas_svc_rec_control_box_chk = $_POST['orders_certs_gas_svc_rec_control_box_chk'];    
     $orders_certs_gas_svc_rec_brnrs_pilot_chk = $_POST['orders_certs_gas_svc_rec_brnrs_pilot_chk'];    
     $orders_certs_gas_svc_rec_fuel_press_typ_chk = $_POST['orders_certs_gas_svc_rec_fuel_press_typ_chk'];   
     $orders_certs_gas_svc_rec_burner_wshd_clnd_opn = $_POST['orders_certs_gas_svc_rec_burner_wshd_clnd_opn'];   
     $orders_certs_gas_svc_rec_pilot_assmbl_clnd_adjst_opn = $_POST['orders_certs_gas_svc_rec_pilot_assmbl_clnd_adjst_opn'];    
     $orders_certs_gas_svc_rec_ignition_sysem_clnd_adjst_opn = $_POST['orders_certs_gas_svc_rec_ignition_sysem_clnd_adjst_opn'];    
     $orders_certs_gas_svc_rec_brner_fan_airway_clnd_opn = $_POST['orders_certs_gas_svc_rec_brner_fan_airway_clnd_opn'];    
     $orders_certs_gas_svc_rec_heat_exch_flu_cln_clr_opn = $_POST['orders_certs_gas_svc_rec_heat_exch_flu_cln_clr_opn'];    
     $orders_certs_gas_svc_rec_fuel_elec_supp_conn_snd_opn = $_POST['orders_certs_gas_svc_rec_fuel_elec_supp_conn_snd_opn'];   
     $orders_certs_gas_svc_rec_intrlck_in_plc_opn = $_POST['orders_certs_gas_svc_rec_intrlck_in_plc_opn'];  
     $orders_certs_gas_svc_rec_appliance_id = $_POST['orders_certs_gas_svc_rec_appliance_id'];  // foreign key 
     $orders_certs_gas_svc_rec_appt_id = $_POST['orders_certs_gas_svc_rec_appt_id'];  // foreign key 
     $orders_certs_gas_svc_rec_eng_sig = $_POST['orders_certs_gas_svc_rec_eng_sig']; 
     $orders_certs_gas_svc_rec_date = $_POST['orders_certs_gas_svc_rec_date']; 
     $orders_certs_gas_svc_rec_notes = $_POST['orders_certs_gas_svc_rec_notes']; 


     $sql="INSERT INTO tbl_orders_certs_gas_svc_rec 
      (orders_certs_gas_svc_rec_fl_atms_fan_assst_fan_diltn_chk,orders_certs_gas_svc_rec_vent_sz_h_l_chk,orders_certs_gas_svc_rec_wat_fuel_snd_chk, 
      orders_certs_gas_svc_rec_elec_fus_chk,orders_certs_gas_svc_rec_correct_valv_arrng_chk,orders_certs_gas_svc_rec_isol_avail_elec_fuel_1met_chk, 
      orders_certs_gas_svc_rec_boilr_plntrm_cln_clr_chk,orders_certs_gas_svc_rec_heat_exch_chk,orders_certs_gas_svc_rec_ignit_chk, 
      orders_certs_gas_svc_rec_gas_vlv_chk,orders_certs_gas_svc_rec_fan_chk,orders_certs_gas_svc_rec_safety_dev_chk,orders_certs_gas_svc_rec_control_box_chk, 
      orders_certs_gas_svc_rec_brnrs_pilot_chk,orders_certs_gas_svc_rec_fuel_press_typ_chk,orders_certs_gas_svc_rec_burner_wshd_clnd_opn, 
      orders_certs_gas_svc_rec_pilot_assmbl_clnd_adjst_opn,orders_certs_gas_svc_rec_ignition_sysem_clnd_adjst_opn,orders_certs_gas_svc_rec_brner_fan_airway_clnd_opn, 
      orders_certs_gas_svc_rec_heat_exch_flu_cln_clr_opn,orders_certs_gas_svc_rec_fuel_elec_supp_conn_snd_opn,orders_certs_gas_svc_rec_intrlck_in_plc_opn, 
      orders_certs_gas_svc_rec_appliance_id,orders_certs_gas_svc_rec_appt_id,orders_certs_gas_svc_rec_eng_sig,orders_certs_gas_svc_rec_date, 
      orders_certs_gas_svc_rec_notes)"; 

     $sql.=" VALUES ('"._FormatStr4Qry($orders_certs_gas_svc_rec_fl_atms_fan_assst_fan_diltn_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_vent_sz_h_l_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_wat_fuel_snd_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_elec_fus_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_correct_valv_arrng_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_isol_avail_elec_fuel_1met_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_boilr_plntrm_cln_clr_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_heat_exch_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_ignit_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_gas_vlv_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_fan_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_safety_dev_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_control_box_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_brnrs_pilot_chk)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_fuel_press_typ_chk)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_burner_wshd_clnd_opn)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_pilot_assmbl_clnd_adjst_opn)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_ignition_sysem_clnd_adjst_opn)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_brner_fan_airway_clnd_opn)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_heat_exch_flu_cln_clr_opn)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_fuel_elec_supp_conn_snd_opn)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_intrlck_in_plc_opn)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_appliance_id)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_appt_id)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_eng_sig)."', 
     '"._FormatStr4Qry($orders_certs_gas_svc_rec_date)."','"._FormatStr4Qry($orders_certs_gas_svc_rec_notes)."')"; 

      if ($res=sqlsrv_query($db,$sql,array(),array("Scrollable"=>'static'))) 
       { 

        // successfully inserted into database 
        $response["success"] = 1; 
        $response["message"] = "Product successfully created."; 

        // echoing JSON response 
        echo json_encode($response); 
       } 
      else 
       { 
        // failed to insert row 
        $response["success"] = 0; 
        $response["message"] = "Oops! An error occurred."; 

        // echoing JSON response 
        echo json_encode($response); 
        die(print_r(sqlsrv_errors(), true)); 
       } 
      sqlsrv_close($db); 
    } 
    else 
     { 
     // successfully inserted into database 
     $response["success"] = 0; 
     $response["message"] = "fill in required fields."; 


     // echoing JSON response 
     echo json_encode($response); 
     } 
} 
else 
{ 
    echo "could not connect to database"; 
} 
?> 

Java類:

 JSONObject json = jsonParser.makeHttpRequest(url_check_operations, 
       "POST", params); 


     // writing response to log 
      Log.d("Create Response:", json.toString()); // return as null 

JSONParser:

package com.example.newdatabase; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.List; 


import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.utils.URLEncodedUtils; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.util.Log; 
import android.widget.Toast; 

public class JSONParser { 

InputStream is = null; 
JSONObject jObj = null; 
String json = ""; 

// constructor 
public JSONParser() { 

} 

// function get json from url 
// by making HTTP POST or GET mehtod 
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { 

    // Making HTTP request 
    try { 

     // check for request method 
     if(method == "POST") 
     { 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 


     } 
     else if(method == "GET"){ 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "UTF_8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     }   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "UTF_8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     json = sb.toString(); 
     Log.e("JSON", json); // added this line as stated on the accepted answer on the above linked quesition. 
    } 

    catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 

      try { 
       jObj = new JSONObject(json); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      // TODO Auto-generated catch block 

    // return JSON String 

    return jObj; 

} 
} 

的logcat:

07-18 10:08:59.090: E/Buffer Error(29965): Error converting result java.lang.NullPointerException: lock == null 

07-18 10:08:59.115: E/AndroidRuntime(29965): FATAL EXCEPTION: AsyncTask #1 
07-18 10:08:59.115: E/AndroidRuntime(29965): java.lang.RuntimeException: An error occured while executing doInBackground() 
07-18 10:08:59.115: E/AndroidRuntime(29965): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.lang.Thread.run(Thread.java:841) 
07-18 10:08:59.115: E/AndroidRuntime(29965): Caused by: java.lang.NullPointerException 
07-18 10:08:59.115: E/AndroidRuntime(29965): at com.example.newdatabase.ChecksAndOperations$NewChecksAndOperations.doInBackground(ChecksAndOperations.java:1305) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at com.example.newdatabase.ChecksAndOperations$NewChecksAndOperations.doInBackground(ChecksAndOperations.java:1) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
07-18 10:08:59.115: E/AndroidRuntime(29965): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
07-18 10:08:59.115: E/AndroidRuntime(29965): ... 4 more 

關於Error converting result java.lang.NullPointerException: lock == null, 我曾嘗試激活StrictMode,但似乎沒有任何區別。

我確信在代碼中一切都很完美。它只在需要時才起作用。

+1

是您的查詢返回一些字符串。發佈你的logcat輸出。 – Rujul1993

+0

當它工作時,它輸出'[success]'的消息(在'PHP'代碼中表示)。我只是在等待logcat輸出錯誤,因爲它需要一些時間。 – user3840721

+0

請按照您的要求檢查編輯 – user3840721

回答

0

試試這個請

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("post_variable", YOUR_VARIABLE)); 

JSONObject json = jsonParser.makeHttpRequest(
          POST_COMMENT_URL, "POST", params); 

        // full json response 
        Log.d("Post sending...", json.toString()); 

        // json success element 
        success = json.getInt(PHP_SUCCESS); 
        if (success == 1) { 
         Log.d("Post Added!", json.toString());  
         finish(); 
         return json.getString(PHP_SUCCESS); 
        }else{ 
         Log.d("Error!", json.getString(TAG_MESSAGE)); 
         return json.getString(PHP_SUCCESS); 
+0

對不起,回覆遲了,正在吃午飯。你指的是哪個'YOUR_VARIABLE'? – user3840721

+0

你想發佈什麼? – Jaky71

+0

噢好吧。我的AsyncTask是'static',因此不允許我使用'finish()'? – user3840721