gpt4 book ai didi

java - 错误解析数据 org.json.jsonexception end of input at character 0

转载 作者:行者123 更新时间:2023-11-29 00:27:28 27 4
gpt4 key购买 nike

我一整天都在查看我的代码,但找不到发生这种情况的原因(或者为什么它实际上没有从它应该返回的 MySQL 服务器返回结果。希望一双新的眼睛能找到原因。

Jsonparser 类

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET method
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, "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());
}

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

}
}

我 Activity 中的内部类

class GetUserDetails extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LogIn.this);
pDialog.setMessage("Loading User details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {

// Building Parameters
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("email", EMAIL));
param.add(new BasicNameValuePair("password", PASSWORD));

// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject jsonuser = jsonParser.makeHttpRequest(
url_user_detials, "GET", param);

// check your log for json response
Log.d("User Details", jsonuser.toString());

// json success tag
try {
int success = jsonuser.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = jsonuser
.getJSONArray("product"); // JSON Array

// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();

editor.putInt("USERID", (Integer) product.get("ProfileID"));
editor.commit();
Intent intent = new Intent(LogIn.this, MyLists.class);
LogIn.this.startActivity(intent);
finish();
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}


/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}

最后是服务器上的 PHP 脚本与数据库对话

    <?php

/*
* Following code will get single product details
* A product is identified by product id (pid)
*/

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET['email'] && isset($_GET['password')) {
$email = $_GET['email'];
$password = $_GET['password'];

// get a product from products table
$result = mysql_query("SELECT * FROM profiles_profiles WHERE ProfileEmail = '$email' AND ProfilePassword = '$password'");

if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {

$result = mysql_fetch_array($result);

$product = array();
$product["ProfileID"] = $result["ProfileID"];
// success
$response["success"] = 1;

// user node
$response["product"] = array();

array_push($response["product"], $product);

// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";

// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";

// echo no users JSON
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);
}
?>

以及我得到的实际错误的堆栈跟踪

08-18 18:43:55.573: E/JSON Parser(16008): Error parsing data org.json.JSONException: End of input at character 0 of 
08-18 18:43:55.573: W/dalvikvm(16008): threadid=13: thread exiting with uncaught exception (group=0x41515450)
08-18 18:43:55.584: E/AndroidRuntime(16008): FATAL EXCEPTION: AsyncTask #1
08-18 18:43:55.584: E/AndroidRuntime(16008): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 18:43:55.584: E/AndroidRuntime(16008): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-18 18:43:55.584: E/AndroidRuntime(16008): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.lang.Thread.run(Thread.java:856)
08-18 18:43:55.584: E/AndroidRuntime(16008): Caused by: java.lang.NullPointerException
08-18 18:43:55.584: E/AndroidRuntime(16008): at com.test.app.LogIn$GetUserDetails.doInBackground(LogIn.java:149)
08-18 18:43:55.584: E/AndroidRuntime(16008): at com.test.app.LogIn$GetUserDetails.doInBackground(LogIn.java:1)
08-18 18:43:55.584: E/AndroidRuntime(16008): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-18 18:43:55.584: E/AndroidRuntime(16008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-18 18:43:55.584: E/AndroidRuntime(16008): ... 5 more

PHP 错误

PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/sites/pmclient.co.uk/public_html/dadgapp/get_user_details.php on line 19

最佳答案

你忘了“;”在:

$password = $_GET['password']


PHP 代码中的行。

关于java - 错误解析数据 org.json.jsonexception end of input at character 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com