通过php检索android中的长文本

nxowjjhe  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(346)

我有一个长文本(一本书的维度)存储在mysql数据库中,我必须通过php网络服务器在android应用程序中下载。
这是我使用的json解析器(部分来自本教程):

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;

    }
}

如果我尝试在一次调用中返回它,则会出现以下错误:
电子邮件/json 解析器:分析数据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个调用,然后是一些随机调用),而另一个调用返回与以前相同的错误。
Web服务器存储在1和1宿主服务中,这可能是因为它们对服务器调用设置了限制?是因为我的代码中有错误吗?你还建议我用什么方法来检索文本?
提前谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题