在研究问题时,我遇到了这个问题(Returned JSONObject is always null),并将接受的答案应用于我的代码。当我连续两次测试它时确实有效,但是在早上醒来测试它最后一次继续运行并且它再次开始返回null。
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
,但似乎没有任何区别。
我确信代码中的一切都很完美。它只是在它想要的时候工作。
答案 0 :(得分: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);