0
嘗試從PHP傳輸長字符串(Base64編碼圖像)到Java時,只傳遞一小部分數據。當我在我的localhost apache服務器上測試應用程序時,字符串被完全發送並且應用程序正常工作。但是,當我將代碼遷移到外部服務器時,只有一小部分數據是用Java接收的。當我在瀏覽器中輸出數據時,長字符串會正確顯示。從PHP傳輸到Java時數據不完整
這是我用來發送數據的代碼:
//get the data from mysql result
while($row = mysqli_fetch_assoc($result))
{
// get the fotopath
$fotoRow = mysqli_fetch_array ($fotoResult, MYSQL_NUM);
$foto = $fotoRow[0];
//replace backslashes
$fotoFormatted = $foto; //str_replace($backslash, $slash, $foto);
//get the picture data
$im = file_get_contents("http://www.mywebsite.com" . $fotoFormatted, true);
//encode the picture data
$imdata = base64_encode($im);
//add the encoded string to the data array
$row ['foto'] = $imdata;
$output[] = $row;
}
//output the data as a json array
print(json_encode($output))
這是我用來接收數據的代碼:
//get the url to the script to connect with
String link = "http://mywebsite/script.php";
//get the data to send to the script
String data = URLEncoder.encode("userid", "UTF-8") + "="
+ URLEncoder.encode(userid, "UTF-8");
//make a new HttpURLConnection
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
//make a new OutputStreamWriter with the HttpURLConnection
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
//write the data and flush the remaining data
wr.write(data);
wr.flush();
//make a new BufferedReader with the given connection
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
//define variables
JSONArray jsonArray = null;
String line = null;
//read the data from the server
while ((line = reader.readLine()) != null) {
jsonArray = new JSONArray(line);
}
//disconnect the connection
conn.disconnect();
//return the data for further use
return jsonArray;
我的問題是:爲什麼數據從外部服務器不完整,而從本地服務器收到的數據是完整的,我能做些什麼來解決這個問題。
您是否通過直接在瀏覽器中打開URL來檢查您是否從PHP腳本獲取完整數據? – kaysush 2015-01-21 10:15:53
爲了擴展kaysuh的問題,你調試了你的Java代碼的值嗎? – reporter 2015-01-21 10:18:49