While trying to send a long string (a Base64 encoded image) from php to java only a fraction of the data is passed over. When I tested the application on my localhost apache server the string was sent over completely and the application worked properly. But when I migrated the code to an external server, only a fraction of the data was received in Java. When I output the data in the browser the long string does display properly.
This is the code I have used to send the data:
//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))
This is the code I have used to receive the data:
//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;
My question is: Why is the data from the external server incomplete while the data received from the local server is complete and what can I do to solve this problem.
I have found the solution, the problem was that spaces were included in the path of some files(pretty bad design i think), because of this a 404 page was being encoded in base64 and in this way the incorrect data was added. in my local server application i had not configured a 404 page so it would just return null and i had a fallback to handle that situation. I replaced all spaces with %20 and the errors were resolved.