Java E/JSON解析器(1506):解析数据时出错[输入结束,字符0处]
我的应用程序,用于将用户输入数据传输到我的SQL数据库。我应该填写所有字段,然后单击保存,以便在我的sql数据库中更新数据。在清理了很多错误之后,我终于陷入了一个我无法解决的错误。非常感谢您提供的任何帮助。请告知我可以进行哪些更改以消除错误。错误是Java E/JSON解析器(1506):解析数据时出错[输入结束,字符0处],java,php,json,Java,Php,Json,我的应用程序,用于将用户输入数据传输到我的SQL数据库。我应该填写所有字段,然后单击保存,以便在我的sql数据库中更新数据。在清理了很多错误之后,我终于陷入了一个我无法解决的错误。非常感谢您提供的任何帮助。请告知我可以进行哪些更改以消除错误。错误是 08-04 05:47:48.799: E/JSON Parser(1506): Error parsing data [End of input at character 0 of ] 我的代码如下: private Pr
08-04 05:47:48.799: E/JSON Parser(1506): Error parsing data [End of input at character 0 of ]
我的代码如下:
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);
// button click event
btnRegisterfine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewFine().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
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 log cat from response
//Log.d("Create Response", json.toString());
// check for success tag
try {
//if (json.getString(TAG_SUCCESS) != null) {
if(json != null && !(json).isNull(TAG_SUCCESS)){
registerFine.setText("");
String success = json.getString(TAG_SUCCESS);
// int success = json.getInt(TAG_SUCCESS);
// if (success == 1) {
if(Integer.parseInt(success) == 1){
// 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;
}
private ProgressDialog pDialog;
JSONParser JSONParser=新的JSONParser();
编辑文本输入驱动程序;
编辑文本输入许可证;
编辑文本输入管理器;
编辑文本输入速度;
编辑文本FineAppl;
编辑文本输入类别;
文本视图注册表项;
//创建新罚款的url
私有静态字符串url_create_fine=”http://192.168.1.1/android_api/create.php";
//JSON节点名称/
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
速度=(EditText)findViewById(R.id.editText3);
Fine=(TextView)findViewById(R.id.editText4);
btnSelectDate=(按钮)findViewById(R.id.buttonSelectDate);
btnSelectTime=(按钮)findViewById(R.id.buttonSelectTime);
inputDriver=(EditText)findViewById(R.id.editText1);
InputLicense=(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);
//创建按钮
按钮btnRegisterfine=(按钮)findViewById(R.id.savefine);
//按钮点击事件
btnRegisterfine.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//在后台线程中创建新产品
新建CreateNewFine().execute();
}
});
}
/**
*创建新产品的后台异步任务
* */
类CreateNewFine扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(FineCalc.this);
pDialog.setMessage(“注册罚款…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
字符串驱动程序=inputDriver.getText().toString();
字符串licencenum=InputLicense.getText().toString();
String officer=inputOfficer.getText().toString();
字符串速度=inputSpeed.getText().toString();
字符串fine=FineAppl.getText().toString();
字符串类别=inputCategory.getText().toString();
//建筑参数
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“驱动程序”,驱动程序));
添加(新的BasicNameValuePair(“licencenum”,licencenum));
参数添加(新的BasicNameValuePair(“高级职员”),高级职员);
参数添加(新的BasicNameValuePair(“速度”,速度));
参数添加(新的BasicNameValuePair(“精细”,精细));
参数添加(新的BasicNameValuePair(“类别”,类别));
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url_create_fine,“POST”,params);
//从响应中检查日志cat
//d(“创建响应”,json.toString());
//检查成功标签
试一试{
//if(json.getString(TAG_SUCCESS)!=null){
if(json!=null&&!(json).isNull(TAG_SUCCESS)){
registerFine.setText(“”);
String success=json.getString(TAG_success);
//int success=json.getInt(TAG_success);
//如果(成功==1){
if(Integer.parseInt(success)==1){
//已成功创建产品
//Intent i=newintent(getApplicationContext(),UserLogin.class);
//星触觉(i);
registerFine.setText(“成功”);
//关闭此屏幕
完成();
}否则{
}//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
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, HTTP.UTF_8), 8);
//BufferedReader reader = new BufferedReader(new InputStreamReader(
// is, HTTP.UTF_8), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} 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) {
// Log.e("JSON Parser", "Error parsing data " + e.toString());
Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);
}
// return JSON String
return jObj;
}
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['driver'], $_POST['licencenum'], $_POST['officer'], $_POST['speed'] , $_POST['fine'],$_POST['category'])){
$driver = $_POST['driver'];
$licencenum = $_POST['licencenum'];
$officer = $_POST['officer'];
$speed = $_POST['speed'];
$fine = $_POST['fine'];
$category = $_POST['category'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO fineregister(driver,licencenum,officer,speed,fine,category) VALUES ('$driver','$licencenum','$officer','$speed','$fine', '$category')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Speed Ticket Successfully Registered.";
// 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);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
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");