javascript - 尝试获取表行计数值,但转换结果时出错 java.lang.NullPointerException : lock == null

标签 javascript php android json

我是这方面的新手,所以我需要帮助解决当前的问题,我正在尝试获取表行计数值,但“解析数据 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 文件中,它返回 null 值。 所以我需要有关 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;
}


?>

最佳答案

删除 printf 行并替换如下

$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;

关于javascript - 尝试获取表行计数值,但转换结果时出错 java.lang.NullPointerException : lock == null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433574/

相关文章:

php - 使用 PayPal IPN 时的客户信息

android - 第三方安卓市场

android - 如何通过 adb 获取 Android 设备的供应商 ID?

php - Google Visualization API 中的逗号分隔数据

javascript - 不会删除 react 中的正确项目

javascript - 如何将 "camelCase"转换为 "Camel Case"?

php - $_POST 在它看到的第一个空格处停止

php - strip <br>(n12br) 从数据库字段获取

android - 如何创建没有边距的 ViewPager?

javascript - html5 电影播放列表,如何计算文件数