Php 如何解决JSON错误:解析数据时出错[输入结束,字符0处]
我是这一级别编程的新手,希望学习JSON并了解其工作原理。我遇到了这个用户关于堆栈溢出的问题,想知道如何以及在哪里使用给他的答案 用户代码为:Php 如何解决JSON错误:解析数据时出错[输入结束,字符0处],php,json,Php,Json,我是这一级别编程的新手,希望学习JSON并了解其工作原理。我遇到了这个用户关于堆栈溢出的问题,想知道如何以及在哪里使用给他的答案 用户代码为: private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); EditText inputDriver; EditText inputLicence; EditText inputOfficer; EditText in
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputDriver;
EditText inputLicence;
EditText inputOfficer;
EditText inputSpeed;
EditText FineAppl;
EditText inputCategory;
TextView registerFine;
// url to create new fine
private static String url_create_fine = "http://192.168.1.1/android_api/create.php";
// JSON Node names/
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speed = (EditText) findViewById(R.id.editText3);
Fine = (TextView)findViewById(R.id.editText4);
btnSelectDate=(Button)findViewById(R.id.buttonSelectDate);
btnSelectTime=(Button)findViewById(R.id.buttonSelectTime);
inputDriver = (EditText) findViewById(R.id.editText1);
inputLicence = (EditText) findViewById(R.id.editText2);
inputOfficer = (EditText) findViewById(R.id.editText5);
inputSpeed = (EditText) findViewById(R.id.editText3);
FineAppl = (EditText) findViewById(R.id.editText4);
inputCategory = (EditText) findViewById(R.id.editText6);
registerFine = (TextView) findViewById(R.id.fineregistered);
// Create button
Button btnRegisterfine = (Button) findViewById(R.id.savefine);
class CreateNewFine extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FineCalc.this);
pDialog.setMessage("Registering Fine..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String driver = inputDriver.getText().toString();
String licencenum = inputLicence.getText().toString();
String officer = inputOfficer.getText().toString();
String speed = inputSpeed.getText().toString();
String fine= FineAppl.getText().toString();
String category = inputCategory.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("driver", driver));
params.add(new BasicNameValuePair("licencenum", licencenum));
params.add(new BasicNameValuePair("officer", officer));
params.add(new BasicNameValuePair("speed", speed));
params.add(new BasicNameValuePair("fine", fine));
params.add(new BasicNameValuePair("category", category));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_fine, "POST", params);
// check for success tag
try {
if(json != null && !(json).isNull(TAG_SUCCESS)){
//registerFine.setText("");
String success = json.getString(TAG_SUCCESS);
if(success != null && success.length() > 0){
// successfully created product
Intent i = new Intent(getApplicationContext(), UserLogin.class);
startActivity(i);
//registerFine.setText("Successful");
// closing this screen
finish();
}
else {
} // failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
有人给出的答案是实施以下步骤。那么,在上面的代码中,如何以及在何处实现以下内容呢
HttpResponse response = client.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
pass the string into json array and get the response value
JSONArray jsArray = new JSONArray(responseBody);
JSONObject js = jsArray.getJSONObject(0);
String returnvalmsg = js.getString("message");
String returnvalsucc = js.getString("success");
构造函数不会返回数据库连接,一旦拥有对象,就可以访问返回DB连接的方法:
require_once 'include/DB_Connect.php';
$db = new DB_Connect();
$conn = $db->connect();
$result = mysql_query("INSERT INTO fineregister(driver,licencenum,officer,speed,fine,category)
VALUES('$driver','$licencenum','$officer','$speed','$fine','$category')"
, $conn);
提供有效:异常表示未完成此操作。(这些代码都不重要,但JSON没有显示出来。)伙计,这里不要问这个问题。在你提到的问题的评论中问这个问题。不幸的是,我还没有足够的声誉来发表评论,所以我想通过在自己的帖子上发表评论来学习。将不胜感激
08-04 05:47:48.799: E/JSON Parser(1506): Error parsing data [End of input at character 0 of ]
HttpResponse response = client.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
pass the string into json array and get the response value
JSONArray jsArray = new JSONArray(responseBody);
JSONObject js = jsArray.getJSONObject(0);
String returnvalmsg = js.getString("message");
String returnvalsucc = js.getString("success");
require_once 'include/DB_Connect.php';
$db = new DB_Connect();
$conn = $db->connect();
$result = mysql_query("INSERT INTO fineregister(driver,licencenum,officer,speed,fine,category)
VALUES('$driver','$licencenum','$officer','$speed','$fine','$category')"
, $conn);