为什么如果我向服务器上的数据库请求超过 100 行,结果却什么也得不到?
Why if I request more than 100 rows to my database on the server do I get nothing as a result?
我正在尝试从我的服务器接收整个 table 数据到我的应用程序。这个功能我只需要在第一次访问时能够在设备上加载分配的 table 并在将来使用新数据动态更新它。
我遇到的问题是 table 由 600 行组成,当我从我的 php 文件中生成 json 时,我在 Android 的答案 reader 中生成了 json就是empty.I想办法接收整个table但是解决不了,希望大家帮帮我。
如果我将行的值减少到 100,我没有问题,但我不能全部发送,我想知道是否有另一种方法可以接收整个 table 或更大的文件。
这是我在应用程序中的 class 调用服务器和 php 文件以提取 table:
private String makeHttpRequestJsonListSpecific(String languageTOpass, String urlAdress) {
String responce=null;
Log.v(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific : SEND MY REQUEST....");
tringBuffer data_post = new StringBuffer();
try {
data_post.append(URLEncoder.encode("Language","UTF-8"));
data_post.append("=");
data_post.append(URLEncoder.encode(languageTOpass,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String json = "";
InputStream responseStream = null;
String logMess = "";
long startTime;
long stopTime;
long elapsedTime;
try {
byte[] postDataBytes = data_post.toString().getBytes("UTF-8");
JSONObject Language_Json = new JSONObject();
Language_Json.put("Language",languageTOpass);
String Message_send = Language_Json.toString();
Log.v(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific : IL VALORE DI Message_send...." + Message_send);
URL url = new URL(urlAdress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setUseCaches(true);
conn.connect();
OutputStreamWriter request = new OutputStreamWriter(conn.getOutputStream());
request.write(Message_send);
request.flush();
request.close();
InputStream inputStream = (InputStream) conn.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI READR--- " + reader.toString());
StringBuilder result = new StringBuilder();
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI RESULT--- " + result.toString());
String line;
while ((line = reader.readLine()) != null) {
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: SONO NEL CICLO DI SCRITTURA DELLA RISPOSTA--- " + conn);
result.append(line).append("\n");
}
json = result.toString();
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: THE DIMENSION OF THE RECEPTION : " + json.getBytes().length);
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: RESOULT : " + json);
conn.disconnect();
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: Connection --- " + conn);
} catch (IOException | JSONException e ) {
Log.e(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific:ERROR MESSAGE --- " + e.getMessage());
Log.e(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific:ERROR MESSAGE Cause --- " + e.getCause());
}
return json;
}
这是我在服务器上生成 json 文件以发送到我的应用程序的 php 文件:
<? php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
$host ='localhost';
$username ='***';
$pwd ='**';
$db ='MYDB';
$responce =array();
$responce ["success"]= 0 ;
$responce ["message"]= "Error at start";
$json=file_get_contents('php://input');
$obj_json=json_decode($json,TRUE);
if(isset($obj_json)){
$language=$obj_json['Language'];
$connect = mysqli_connect($host,$username,$pwd,$db) or die('Unabletp connect');
if(mysqli_connect_error($connect)){
echo ("Fallita la connessione al data base".mysqli_connect_error());
$responce ['success']=0;
$responce ['message']="NO CONNECTION NEW_SPECIFIC_PRODUCT";
}else{
// I SET MY SEARCH QUERY
// CHECK THAT THE LANGUAGE IS ITALIAN
if(strcasecmp($language,"it")== 0){
$sql_find = ("SELECT FAMILY_PRODUCT,CATEGORY_PRODUCT,SPECIFIC_PRODUCT,NAME_SPECIFIC FROM NEW_SPECIFIC_PRODUCT_IT;") ;
$result= mysqli_query($connect,$sql_find) or die ("Error: ". mysqli_error($connect)." with query ");
if(mysqli_num_rows($result)> 0){
$responce ["success"]= 1 ;
$i=0;
while ($row = mysqli_fetch_array($result)){
$name_specific=$row["NAME_SPECIFIC"];
$result_array[$i]= array("FAMILY_PRODUCT" => $row["FAMILY_PRODUCT"],"CATEGORY_PRODUCT" =>$row["CATEGORY_PRODUCT"],"SPECIFIC_PRODUCT" => $row["SPECIFIC_PRODUCT"],"NAME_SPECIFIC" => $name_specific);
$i++;
}
$responce ['success']=1;
$responce ['message']="I VALORI SONO STATI TROVATI";
}else{
# code...
$responce ['success']=0;
$responce ['message']="NO DATA IN NEW_SPECIFIC_PRODUCT_IT";
}
}else{
// THE TELEPHONE LANGUAGE IS NOT ITALIAN
echo ("IL VALORE DI language non è it.....".$language);
$sql_find = ("SELECT * FROM NEW_SPECIFIC_PRODUCT_EN;") ;
$result= mysqli_query($connect,$sql_find) or die ("Error: ". mysqli_error($connect)." with query ");
if(mysqli_num_rows($result)> 0){
$responce ["success"]= 1 ;
$i=0;
while ($row = mysqli_fetch_array($result)){
$result_array[$i]= array("FAMILY_PRODUCT" =>$row["FAMILY_PRODUCT"],"CATEGORY_PRODUCT"=>$row["CATEGORY_PRODUCT"],"SPECIFIC_PRODUCT"=>$row["SPECIFIC_PRODUCT"],"NAME_SPECIFIC"=>$row["NAME_SPECIFIC"]);
$i++;
}
}else{
# code...
$responce ['success']=0;
$responce ['message']="NO DATA IN NEW_SPECIFIC_PRODUCT_EN";
}
}
mysqli_close($connect);
array_push($responce,$result_array);
}
}else{
$responce['success']=0;
$responce['message']="Parameters are not correct";
echo json_encode($responce);
}
header('Content-type:application/json');
echo json_encode($responce);
?>
当我去验证我发送的 json 结果是空的,当我没有收到任何东西时,是否有另一种方法可以接收整个 table?
在此先感谢您的帮助。
我按要求添加日志文件:
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI READR--- java.io.BufferedReader@---
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI RESULT---
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: LA DIMENSIONE DELLA RICEZIONE : 0
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************
我已经找到问题的解决方案并报告它,以便如果它对某人有用,他们可以使用它。
经过多次检查后,我注意到问题是我的 Json 不是在我的 php 文件中创建的,因此使用我的数据库中由字符串值组成的部分我试图检查 Json 生成错误通过添加此代码立即鼠标生成 Json:
echo json_encode($responce);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
通过这次验证,我的错误类型是“格式错误的 UTF-8 字符,可能编码不正确”
所以我在提取我的查询结果之后插入一个 UTF-8 解码,然后再将它插入我的答案数组,然后我将在 Json 中编码。
这使我能够正确创建 Json 并克服由不允许编码的特定字符指示的块。
$name_specific=utf8_encode($row["NAME_SPECIFIC"]);
希望能帮到和我同街区的朋友,谢谢大家
我正在尝试从我的服务器接收整个 table 数据到我的应用程序。这个功能我只需要在第一次访问时能够在设备上加载分配的 table 并在将来使用新数据动态更新它。 我遇到的问题是 table 由 600 行组成,当我从我的 php 文件中生成 json 时,我在 Android 的答案 reader 中生成了 json就是empty.I想办法接收整个table但是解决不了,希望大家帮帮我。
如果我将行的值减少到 100,我没有问题,但我不能全部发送,我想知道是否有另一种方法可以接收整个 table 或更大的文件。
这是我在应用程序中的 class 调用服务器和 php 文件以提取 table:
private String makeHttpRequestJsonListSpecific(String languageTOpass, String urlAdress) {
String responce=null;
Log.v(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific : SEND MY REQUEST....");
tringBuffer data_post = new StringBuffer();
try {
data_post.append(URLEncoder.encode("Language","UTF-8"));
data_post.append("=");
data_post.append(URLEncoder.encode(languageTOpass,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String json = "";
InputStream responseStream = null;
String logMess = "";
long startTime;
long stopTime;
long elapsedTime;
try {
byte[] postDataBytes = data_post.toString().getBytes("UTF-8");
JSONObject Language_Json = new JSONObject();
Language_Json.put("Language",languageTOpass);
String Message_send = Language_Json.toString();
Log.v(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific : IL VALORE DI Message_send...." + Message_send);
URL url = new URL(urlAdress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setUseCaches(true);
conn.connect();
OutputStreamWriter request = new OutputStreamWriter(conn.getOutputStream());
request.write(Message_send);
request.flush();
request.close();
InputStream inputStream = (InputStream) conn.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI READR--- " + reader.toString());
StringBuilder result = new StringBuilder();
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI RESULT--- " + result.toString());
String line;
while ((line = reader.readLine()) != null) {
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: SONO NEL CICLO DI SCRITTURA DELLA RISPOSTA--- " + conn);
result.append(line).append("\n");
}
json = result.toString();
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: THE DIMENSION OF THE RECEPTION : " + json.getBytes().length);
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************ " );
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: RESOULT : " + json);
conn.disconnect();
Log.d(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific: Connection --- " + conn);
} catch (IOException | JSONException e ) {
Log.e(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific:ERROR MESSAGE --- " + e.getMessage());
Log.e(TAG, "GetSpecificListFromServer: makeHttpRequestJsonListSpecific:ERROR MESSAGE Cause --- " + e.getCause());
}
return json;
}
这是我在服务器上生成 json 文件以发送到我的应用程序的 php 文件:
<? php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL | E_STRICT);
$host ='localhost';
$username ='***';
$pwd ='**';
$db ='MYDB';
$responce =array();
$responce ["success"]= 0 ;
$responce ["message"]= "Error at start";
$json=file_get_contents('php://input');
$obj_json=json_decode($json,TRUE);
if(isset($obj_json)){
$language=$obj_json['Language'];
$connect = mysqli_connect($host,$username,$pwd,$db) or die('Unabletp connect');
if(mysqli_connect_error($connect)){
echo ("Fallita la connessione al data base".mysqli_connect_error());
$responce ['success']=0;
$responce ['message']="NO CONNECTION NEW_SPECIFIC_PRODUCT";
}else{
// I SET MY SEARCH QUERY
// CHECK THAT THE LANGUAGE IS ITALIAN
if(strcasecmp($language,"it")== 0){
$sql_find = ("SELECT FAMILY_PRODUCT,CATEGORY_PRODUCT,SPECIFIC_PRODUCT,NAME_SPECIFIC FROM NEW_SPECIFIC_PRODUCT_IT;") ;
$result= mysqli_query($connect,$sql_find) or die ("Error: ". mysqli_error($connect)." with query ");
if(mysqli_num_rows($result)> 0){
$responce ["success"]= 1 ;
$i=0;
while ($row = mysqli_fetch_array($result)){
$name_specific=$row["NAME_SPECIFIC"];
$result_array[$i]= array("FAMILY_PRODUCT" => $row["FAMILY_PRODUCT"],"CATEGORY_PRODUCT" =>$row["CATEGORY_PRODUCT"],"SPECIFIC_PRODUCT" => $row["SPECIFIC_PRODUCT"],"NAME_SPECIFIC" => $name_specific);
$i++;
}
$responce ['success']=1;
$responce ['message']="I VALORI SONO STATI TROVATI";
}else{
# code...
$responce ['success']=0;
$responce ['message']="NO DATA IN NEW_SPECIFIC_PRODUCT_IT";
}
}else{
// THE TELEPHONE LANGUAGE IS NOT ITALIAN
echo ("IL VALORE DI language non è it.....".$language);
$sql_find = ("SELECT * FROM NEW_SPECIFIC_PRODUCT_EN;") ;
$result= mysqli_query($connect,$sql_find) or die ("Error: ". mysqli_error($connect)." with query ");
if(mysqli_num_rows($result)> 0){
$responce ["success"]= 1 ;
$i=0;
while ($row = mysqli_fetch_array($result)){
$result_array[$i]= array("FAMILY_PRODUCT" =>$row["FAMILY_PRODUCT"],"CATEGORY_PRODUCT"=>$row["CATEGORY_PRODUCT"],"SPECIFIC_PRODUCT"=>$row["SPECIFIC_PRODUCT"],"NAME_SPECIFIC"=>$row["NAME_SPECIFIC"]);
$i++;
}
}else{
# code...
$responce ['success']=0;
$responce ['message']="NO DATA IN NEW_SPECIFIC_PRODUCT_EN";
}
}
mysqli_close($connect);
array_push($responce,$result_array);
}
}else{
$responce['success']=0;
$responce['message']="Parameters are not correct";
echo json_encode($responce);
}
header('Content-type:application/json');
echo json_encode($responce);
?>
当我去验证我发送的 json 结果是空的,当我没有收到任何东西时,是否有另一种方法可以接收整个 table? 在此先感谢您的帮助。
我按要求添加日志文件:
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI READR--- java.io.BufferedReader@---
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: IL VALORE DI RESULT---
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: LA DIMENSIONE DELLA RICEZIONE : 0
GetSpecificListFromServer: makeHttpRequestJsonListSpecific: ************************************************************
我已经找到问题的解决方案并报告它,以便如果它对某人有用,他们可以使用它。 经过多次检查后,我注意到问题是我的 Json 不是在我的 php 文件中创建的,因此使用我的数据库中由字符串值组成的部分我试图检查 Json 生成错误通过添加此代码立即鼠标生成 Json:
echo json_encode($responce);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
通过这次验证,我的错误类型是“格式错误的 UTF-8 字符,可能编码不正确” 所以我在提取我的查询结果之后插入一个 UTF-8 解码,然后再将它插入我的答案数组,然后我将在 Json 中编码。 这使我能够正确创建 Json 并克服由不允许编码的特定字符指示的块。
$name_specific=utf8_encode($row["NAME_SPECIFIC"]);
希望能帮到和我同街区的朋友,谢谢大家