我有一个长文本(书的维度)存储在MySQL数据库在线,我必须通过PHP网络服务器在Android应用程序中下载。
这是我使用的JSON解析器(部分来自this tutorial):
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 mehtod
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"), 10000000);
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.i("json", "here");
Log.i("json", 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;
}
}
如果我尝试在一次调用中返回它,则会出现此错误:
E / JSON Parser:解析数据时出错org.json.JSONException:字符0的输入结束
我认为这是由于文件的维度所以我试图分解当时检索1000个字符的调用(如果在浏览器上单独测试PHP文件的话)。这是我重新编写本书的异步任务的代码:
protected Boolean doInBackground(String... params) {
boolean suc = false;
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> parametri = new ArrayList<NameValuePair>();
parametri.add(new BasicNameValuePair(TAG_BOOKID, Integer.toString(id)));
JSONObject json = jsonParser.makeHttpRequest(
url_book_file_length, "GET", parametri);
// check your log for json response
Log.d("Single Book length", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_BOOK); // JSON Array
// get first product object from JSON Array
JSONObject jbook = productObj.getJSONObject(0);
int length = jbook.getInt(TAG_LENGTH);
int current = 1;
String file = "";
suc = true;
while (current <= length){
parametri = new ArrayList<NameValuePair>();
parametri.add(new BasicNameValuePair(TAG_BOOKID, Integer.toString(id)));
parametri.add(new BasicNameValuePair(TAG_START, Integer.toString(current)));
if (length-current>=LENGTH_MAX){
parametri.add(new BasicNameValuePair(TAG_LENGTH, Integer.toString(LENGTH_MAX)));
current+= LENGTH_MAX;
} else {
parametri.add(new BasicNameValuePair(TAG_LENGTH, Integer.toString(length-current+1)));
current = length+1;
}
json = jsonParser.makeHttpRequest(
url_book_file, "GET", parametri);
// check your log for json response
Log.d("Single Book pezzo file", "current " + Integer.toString(current) + " " + json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
productObj = json
.getJSONArray(TAG_BOOK); // JSON Array
// get first product object from JSON Array
jbook = productObj.getJSONObject(0);
file += jbook.getString(TAG_FILE);
}else{
suc = false;
break;
// product with pid not found
}
}
book.setFile(file);
Log.i("File", "saved");
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return suc;
}
这是PHP文件:
<?php
// 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["bookid"]))&&(isset($_GET["start"]))&&(isset($_GET["length"]))) {
$start = $_GET['start'];
$length = $_GET['length'];
$bookid = $_GET['bookid'];
// get a book from books table
$result = mysql_query("SELECT SUBSTRING(file, $start, $length) AS file FROM book WHERE bookid = $bookid");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$book = array();
$book["file"] = $result["file"];
// success
$response["success"] = 1;
// user node
$response["book"] = array();
array_push($response["book"], $book);
// echoing JSON response
echo json_encode($response);
} else {
// no book found
$response["success"] = 0;
$response["message"] = "No book found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no book found
$response["success"] = 0;
$response["message"] = "No book 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);
}
?>
但是现在,例如当LENGTH_MAX为1000时,只有部分调用成功(特别是前8个然后是一些随机调用),而另一个则返回与之前相同的错误。
网络服务器存储在1and1托管服务中,可能是由于它们在服务器调用上设置的限制?这是我的代码中的错误吗?您建议我使用其他方法来检索文本吗?
提前致谢