试图获得表行计数值,但得到错误转换结果java.lang.NullPointerException: lock == n
trying to get table row count value but got Error converting result java.lang.NullPointerException: lock == null
我是新的,所以我需要帮助我目前的问题,我试图获得表行计数值,但"错误解析数据org.json.JSONException:类型java.lang.String的值结果不能转换为JSONObject"出现
这是我的rowcount。php:
<?php
$mysqli = new mysqli("localhost", "root", "", "smkkimmanuel2");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT * FROM pendaftaran")) {
/* determine number of rows result set */
$row_cnt = $result->num_rows;
printf("Result set has %d rows.'n", $row_cnt);
$response["rowcount"] = $row_cnt;
die(json_encode($response));
}
?>
和row_count_test类:
package id.wanda.smkkkristenimmanuelii;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Row_count_test extends Activity {
private TextView testRowCount;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.110/smkkimmanuel2/rowcount.php";
// JSON element ids from repsonse of php script:
private static final String TAG_ROWCOUNT = "rowcount";
public static String row;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_row_count_test);
testRowCount = (TextView) findViewById(R.id.test);
new RowCount().execute();
}
public void rowCount(View v) {
}
class RowCount extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
String getRow;
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequestRowCount(LOGIN_URL);
try {
getRow = json.getString(TAG_ROWCOUNT);
row = getRow;
} catch (JSONException e) {
e.printStackTrace();
}catch (NullPointerException e) {
e.printStackTrace();
}
return row;
}
@Override
protected void onPostExecute(String row) {
// TODO Auto-generated method stub
testRowCount.setText(row);
}
}
}
这是我的JSONObject @PareshMayani先生:
public JSONObject makeHttpRequestRowCount(String url, String method) {
// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
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, "iso-8859-1"), 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());
}
Log.d("JSON Parser", json);
// 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());
}
// return JSON String
return jObj;
}
编辑:似乎问题发生在PHP文件中,它返回空值。我需要PHP文件
的帮助下面是新的logcat错误:05-26 11:28:46.530: E/Buffer Error(15945): Error converting result java.lang.NullPointerException: lock == null
05-26 11:28:46.530: E/JSON Parser(15945): Error parsing data org.json.JSONException: End of input at character 0 of
下面是新的rowcount.php文件:
<?php
$mysqli = new mysqli("localhost", "root", "", "smkkimmanuel2");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT * FROM pendaftaran")) {
/* determine number of rows result set */
$row_cnt = $result->num_rows;
$response["rowcount"] = $row_cnt;
echo json_encode($response);exit;
}
?>
删除打印行并替换为如下
$row_cnt = $result->num_rows;
// remove this line
//printf("Result set has %d rows.'n", $row_cnt);
$response["rowcount"] = $row_cnt;
//replace this line
//die(json_encode($response));
echo json_encode($response);exit;
你面临两个问题:
1) 05-25 14:43:42.974: E/JSON Parser(23557):解析数据错误org.json.JSONException: Value类型为java.lang.String的结果不能转换为JSONObject
当您尝试从字符串创建JSON对象并且该字符串实际上不是JSON object字符串时,此异常将被触发。
如果你得到{"rowcount":5}
,那么这是正确的JSON对象。
2) java.lang.RuntimeException:执行时发生错误doInBackground ()原因:android.view。ViewRootImpl$ calledfromwrongthreadeexception:只有创建视图层次结构的原始线程才能触摸其视图。
问题描述本身不言自明。仅供参考,由于doInBackground()
中写的下面一行,这个问题正在被提出:
testRowCount.setText(row);
你不能在执行后台操作时更新任何视图(即TextView或任何其他视图)。换句话说,你可以只在主线程中更新UI。
有两种方法:
- 在
doInBackground()
方法中实现runOnUiThread()
,并编写视图更新代码。 - 从
onPostExecute()
方法中更新UI
我终于解决了我的问题,首先我创建了一个config.php连接到数据库
<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "smkkimmanuel2";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
header('Content-Type: text/html; charset=utf-8');
session_start();
?>
并将rowcount.php调整为如下:
<?php
//load and connect to MySQL database stuff
require("config.php");
//gets user's info based off of a username.
$query = "SELECT * FROM pendaftaran_calon_siswa";
try {
$stmt = $db->prepare($query);
$stmt->execute();
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one to product JSON data:
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
//fetching all the rows from the query
$row = $stmt->fetchAll();
$rows = count($row);
$response["rowcount"] = $rows;
echo json_encode($response);
?>
它工作了,不知道它是如何工作的,感谢所有帮助我的人,最终解决了这个问题
- 为什么这个js代码的RSA加密结果与java的RSA标准代码不同
- 在PHP结果中添加复选框,并在一段时间后使用java逐个打开它们
- 如何从批处理文件调用一个带有2个参数的java脚本函数,并将结果返回到环境变量
- 执行java脚本并在我的php查询中使用结果
- JavaScript 中的计算结果与 Java 中的计算结果不同
- 如何在从线程收到一些结果后使用 java 脚本停止自动刷新功能
- java脚本regex.match只查找一个结果
- JS从Java返回结果
- 基于java代码执行结果的确认对话框
- 调用一个打开活动并将活动结果返回给javascript的java函数
- Java和JS:将结果存储为Java变量
- 如何使用Java或Javascript程序从Splunk导出搜索结果
- 如何获得Javascript数组结果到Java
- 如何将值从java传递到.js文件,并在.html中得到结果
- 如何在Android中解析Java脚本提供的结果
- 将java脚本函数结果传递给Angular ng-table
- GWT JSNI - Java到Javascript返回到Java结果未定义的参数
- 如何读取ul列表并将结果发送回java
- Java脚本添加日期不会产生结果
- 试图获得表行计数值,但得到错误转换结果java.lang.NullPointerException: lock == n