I encode Images on server with php and send the encoded strings to android via get request and decode it with java but It outputs bad base64 .
so I decided to check the base64 string on online checker but the image doesn't appear is the problem with the php encoding ?
here is the encoded image string :
aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n
PHP::
<?php
require_once("config.php");
if(isset($_GET["m"])) {
$dirname = "images/main/";
$arr = array();
$conn = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
if(!$conn) {
echo "Error connecting to database";
exit();
}
if($stmt = $conn->prepare("SELECT name_ FROM projects")) {
$stmt->execute();
$stmt->bind_result($n);
//$stmt->store_result();
$result = $stmt->get_result();
if($result->num_rows == 0) {
echo "No Projects";
$stmt->close();
$conn->close();
exit();
}else {
while ($row = $result->fetch_assoc()) {
$dirname = $dirname . $row["name_"] . "/";
$images = glob($dirname . "*.*", GLOB_BRACE);
foreach($images as $key => $image) {
$image = base64_encode($image);
//array_push($arr, $image);
$dirname = "images/main/";
echo $image;
echo "/n";
$image = "";
}
}
//echo "hi";//json_encode($arr);
}
}
$stmt->close();
$conn->close();
exit();
}
?>
ANDROID::
#Override
protected String doInBackground(String[] params) {
String add = "http://10.0.2.2/wael/getimages.php?m=all";
byte[] image = null;
Bitmap real = null;
String parsedString = "";
BufferedReader bufferedReader = null;
InputStream is = null;
StringBuilder sb = null;
HttpURLConnection httpConn = null;
URLConnection conn = null;
try {
URL url = new URL(add);
conn = url.openConnection();
httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
is = httpConn.getInputStream();
sb = new StringBuilder();
String line;
bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
bitmaps = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\\n");
//image = Base64.decode(line, Base64.NO_PADDING);
//real = BitmapFactory.decodeByteArray(image, 0, image.length);
//bitmaps.add(real);
//image = null;
//real = null;
}
String[] lines = sb.toString().split("\\n");
for(String s: lines){
image = Base64.decode(s, Base64.URL_SAFE);
real = BitmapFactory.decodeByteArray(image, 0, image.length);
bitmaps.add(real);
image = null;
real = null;
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (is != null) {
is.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
if (httpConn != null) {
httpConn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "done";
}
HINT::
all types of modes in decoding using java are not working . for exmaple URL_SAFE , DEFAULT and etc arenot working all output the same bad base64
You aren't base64 encoding an image, you are base64 encoding the path to an image.
It's like if someone said "Send me a picture of the empire state building", and then you wrote them a letter that said "A picture of the empire state building".
For example, in your comment you said
the following is still not outputing an image aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n
But if I do the following:
console.log(atob("aW1hZ2VzL21haW4vd3d3L25ldCAtIENvcHkucG5n"))
when you hit run you will see
images/main/www/net - Copy.png
Clearly, not the intended result.
In PHP, you would instead do something like this:
$image = file_get_contents($filename);
$b64_image_raw = base64_encode($im);
$mime = mime_content_type($filetype)
$b64_image = "data:" . $mime . ";base64," . $b56_image_raw;
which will give you something like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAMAAABhTZc9AAAAP1BMVEU7V507V50AAAA7V507V507V53///9GYaP7/P33+Pvm6vPa4O2aqM3s7/bf5O+8xd6uutigrdBfd7BNZ6c+Wp9WPQrIAAAABXRSTlP0cwDze/4T5ZQAAABkSURBVCjP3c45DoAwDETRcUiclZ37n5UoFUUyEhIVv32yNTAyuX6TGIgbJwDRiqy36kuOGlNfj6StrvpZie7KdKmw+dGqUPUcbm5PP1d9FC6mmd6uVAtf9VcFUcAStTCCgULMDXttET4Wr4wGAAAAAElFTkSuQmCC
var image = document.createElement("img")
image.src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAMAAABhTZc9AAAAP1BMVEU7V507V50AAAA7V507V507V53///9GYaP7/P33+Pvm6vPa4O2aqM3s7/bf5O+8xd6uutigrdBfd7BNZ6c+Wp9WPQrIAAAABXRSTlP0cwDze/4T5ZQAAABkSURBVCjP3c45DoAwDETRcUiclZ37n5UoFUUyEhIVv32yNTAyuX6TGIgbJwDRiqy36kuOGlNfj6StrvpZie7KdKmw+dGqUPUcbm5PP1d9FC6mmd6uVAtf9VcFUcAStTCCgULMDXttET4Wr4wGAAAAAElFTkSuQmCC";
document.getElementById("image").appendChild(image)
<div id="image"></div>
Related
I have a json file which contains only objects now i cant able to fetch all the object with iteration as here is no array.
Here is the code : That i have tried in my android studio & JSon Data Screenshot
#Override
protected String doInBackground(String... strings) {
try {
url = new URL("https://api.myjson.com/bins/r7dj6");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line =" ";
while ((line = bufferedReader.readLine()) != null){
stringBuffer.append(line);
}
String fullfile = stringBuffer.toString();
JSONObject jsonObject = new JSONObject(fullfile);
JSONObject jsonObjectchild = jsonObject.getJSONObject("Apartment");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
Here is a complete example. Works for me.
HttpURLConnection connection = null;
try {
URL url = new URL("https://api.myjson.com/bins/r7dj6");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder stringBuffer = new StringBuilder();
while ((line = reader.readLine()) != null){
stringBuffer.append(line);
}
JSONObject jsonObject = new JSONObject(stringBuffer.toString());
JSONObject apartmentObject = jsonObject.getJSONObject("Apartment");
Iterator<String> keys = apartmentObject.keys();
while (keys.hasNext()) {
String flatName = keys.next();
JSONObject flat = apartmentObject.getJSONObject(flatName);
String age = flat.getString("age");
String color = flat.getString("color");
String name = flat.getString("name");
String owner = flat.getString("owner");
String partner = flat.getString("partner");
Log.d("Flat", flatName + ": " + age + ", " + color + ", " + name + ", " + owner + ", " + partner);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
You can still iterate:
for (Iterator key=jsonObjectchild.keys();key.hasNext();) {
JSONObject flatName = json.get(key.next());
...
}
I'm getting following error in my app
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
The JAVA Code where a HTTP Connection is started looks like this:
boolean l = true;
String[] ergebnisArray = new String[100];
for(int i = 0; i < ergebnisArray.length; i++) {
ergebnisArray[i] = "";
}
String count = "0";
if(l) {
try {
String login_url = "http://192.168.178.82:8080/webapp/test.php";
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data = URLEncoder.encode("count", "UTF-8") + "=" + URLEncoder.encode(count, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
if (response != "") {
int c = Integer.parseInt(count);
ergebnisArray[c] = response;
c++;
count = Integer.toString(c);
} else {
l = false;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return ergebnisArray;
And finally the PHP Code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$zaehler = $_POST["count"];
$sql = "SELECT * FROM stundenplan";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() ) {
for ($i = 0; $i <= (int)$zaehler; $i++) {
$output = "".$row["Lehrer"]." ".$row["Klasse"]." ".$row["Stunde"];
}
}
} else {
echo "";
}
echo $output;
$conn->close();
?>
Thanks in advance
You need to declare $output outside of function then put data inside function or you may print it inside for loop.
I think there is a problem of variable scope something like global or local.
I have coded a web crawler. But when crawling it downloads too many GBs of data.
I want to read only the text (avoiding images ...etc).
I use Boilerpipe to extract the content from html
Here is how I find the final redirected url
public String getFinalRedirectedUrl(String url) throws IOException{
HttpURLConnection connection;
String finalUrl = url;
int redirectCount = 0;
do {
connection = (HttpURLConnection) new URL(finalUrl)
.openConnection();
connection.setConnectTimeout(Config.HTTP_CONNECTION_TIMEOUT_TIME);
connection.setReadTimeout(Config.HTTP_READ_TIMEOUT_TIME);
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 300 && responseCode < 400) {
String redirectedUrl = connection.getHeaderField("Location");
if (null == redirectedUrl)
break;
finalUrl = redirectedUrl;
redirectCount++;
if(redirectCount > Config.MAX_REDIRECT_COUNT){
throw new java.net.ProtocolException("Server redirected too many times ("+Config.MAX_REDIRECT_COUNT+")");
}
} else{
break;
}
} while (connection.getResponseCode() != HttpURLConnection.HTTP_OK);
connection.disconnect();
return finalUrl;
}
This is how I fetch the url
private HTMLDocument fetch(URL url) throws IOException{
final HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.setFollowRedirects(true);
httpcon.setConnectTimeout(Config.HTTP_CONNECTION_TIMEOUT_TIME);
httpcon.setReadTimeout(Config.HTTP_READ_TIMEOUT_TIME);
httpcon.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2");
final String ct = httpcon.getContentType();
Charset cs = Charset.forName("Cp1252");
if (ct != null) {
if(!ct.contains("text/html")){
System.err.println("Content type is:"+ct);
return new HTMLDocument("");
}
Matcher m = PAT_CHARSET.matcher(ct);
if(m.find()) {
final String charset = m.group(1);
try {
cs = Charset.forName(charset);
} catch (UnsupportedCharsetException | IllegalCharsetNameException e) {
// keep default
}
}
}
InputStream in = httpcon.getInputStream();
final String encoding = httpcon.getContentEncoding();
if(encoding != null) {
if("gzip".equalsIgnoreCase(encoding)) {
in = new GZIPInputStream(in);
} else {
System.err.println("WARN: unsupported Content-Encoding: "+encoding);
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int r;
while ((r = in.read(buf)) != -1) {
bos.write(buf, 0, r);
}
in.close();
final byte[] data = bos.toByteArray();
return new HTMLDocument(data, cs);
}
And to get the body using Boilerpipe
HTMLDocument htmlDoc = fetch(new URL(url));
String body = ArticleExtractor.INSTANCE.getText(htmlDoc.toInputSource());
How to reduce the amount of data downloaded?
Reduced the GB downloaded and increased the efficiency by using JSoup
public HashMap<String, String> fetchWithJsoup(String url, String iniUrl, int redirCount)
throws IOException
{
HashMap<String, String> returnObj = new HashMap<>();
Connection con;
try{
con = Jsoup.connect(url);
}catch(IllegalArgumentException ex){
if(ex.getMessage().contains("Malformed URL")){
System.err.println("Malformed URL:: "
+ex.getClass().getName()+": "+ex.getMessage()+" > "+iniUrl);
}else{
Logger.getLogger(ContentGetter.class.getName()).log(Level.SEVERE, null, ex);
}
returnObj.put(RETURN_FINAL_URL, url);
returnObj.put(RETURN_BODY, "");
return returnObj;
}
con.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2");
con.timeout(Config.HTTP_CONNECTION_TIMEOUT_TIME);
Document doc = con.get();
String uri = doc.baseUri();
returnObj.put(RETURN_FINAL_URL, uri);
Elements redirEle = doc.head().select("meta[http-equiv=refresh]");
if(redirEle.size() > 0){
String content = redirEle.get(0).attr("content");
Pattern pattern = Pattern.compile("^.*URL=(.+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if (matcher.matches() && matcher.groupCount() > 0) {
String redirectUrl = matcher.group(1);
if(redirectUrl.startsWith("'")){
/*removes single quotes of urls within single quotes*/
redirectUrl = redirectUrl.replaceAll("(^')|('$)","");
}
if(redirectUrl.startsWith("/")){
String[] splitedUrl = url.split("/");
redirectUrl = splitedUrl[0]+"//"+splitedUrl[2]+redirectUrl;
}
if(!redirectUrl.equals(url)){
redirCount++;
if(redirCount < Config.MAX_REDIRECT_COUNT){
return fetchWithJsoup(redirectUrl, iniUrl, redirCount);
}
}
}
}
HTMLDocument htmlDoc = new HTMLDocument(doc.html());
String body = "";
try{
if(htmlDoc != null){
body = ArticleExtractor.INSTANCE.getText(htmlDoc.toInputSource());
}
}catch(OutOfMemoryError ex){
System.err.println("OutOfMemoryError while extracting text !!!!!!!!");
System.gc();
} catch (BoilerpipeProcessingException ex) {
Logger.getLogger(ContentGetter.class.getName()).log(Level.SEVERE, null, ex);
}
returnObj.put(RETURN_BODY, body);
return returnObj;
}
I'm using below codes for downloading data from Google and getting a location keyword lat lng in my monodroid application:
public LatLng GetLatLng (String input)
{
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder ();
LatLng gp = null;
string str = null;
try {
StringBuilder sb = new StringBuilder (GEOCODER_API_BASE + OUT_JSON);
sb.Append ("?address=" + input + "&sensor=true");
/// "http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true"
URL url = new URL (sb.ToString ());
conn = (HttpURLConnection)url.OpenConnection ();
Java.IO.InputStreamReader inp = new Java.IO.InputStreamReader (conn.InputStream);
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = inp.Read (buff)) != -1) {
jsonResults.Append (buff, 0, read);
}
str = jsonResults .ToString();
} catch (System.IO.IOException e) {
RltLog .HandleException (e, "\nError connecting to Places API\n");
return gp;
} finally {
if (conn != null) {
conn.Disconnect ();
}
}
try {
// Create a JSON object hierarchy from the results
JObject address = JObject .Parse (str);
JArray ja = (JArray)address ["results"];
JObject LocationJObejct = (JObject)ja [0] ["geometry"] ["location"];
gp = HelperMethods .executeLocationPoint (LocationJObejct ["lat"].ToString (),
LocationJObejct ["lng"].ToString ());
} catch (Exception e) {
RltLog .HandleException (e);
}
return gp;
}
But I keep getting these error:
Exception of type 'Java.IO.FileNotFoundException' was thrown.
I'm wondering what is the reason?
You should replace spaces with + in your request based on the google docs.
I'm having trouble getting the compressed jpeg image (stored as a blob in my database).
here is the snippet of code I use to output the image that I have in my database:
if($row = mysql_fetch_array($sql))
{
$size = $row['image_size'];
$image = $row['image'];
if($image == null){
echo "no image!";
} else {
header('Content-Type: content/data');
header("Content-length: $size");
echo $image;
}
}
here is the code that I use to read in from the server:
URL sizeUrl = new URL(MYURL);
URLConnection sizeConn = sizeUrl.openConnection();
// Get The Response
BufferedReader sizeRd = new BufferedReader(new InputStreamReader(sizeConn.getInputStream()));
String line = "";
while(line.equals("")){
line = sizeRd.readLine();
}
int image_size = Integer.parseInt(line);
if(image_size == 0){
return null;
}
URL imageUrl = new URL(MYIMAGEURL);
URLConnection imageConn = imageUrl.openConnection();
// Get The Response
InputStream imageRd = imageConn.getInputStream();
byte[] bytedata = new byte[image_size];
int read = imageRd.read(bytedata, 0, image_size);
Log.e("IMAGEDOWNLOADER", "read "+ read + " amount of bytes");
Log.e("IMAGEDOWNLOADER", "byte data has length " + bytedata.length);
Bitmap theImage = BitmapFactory.decodeByteArray(bytedata, 0, image_size);
if(theImage == null){
Log.e("IMAGEDOWNLOADER", "the bitmap is null");
}
return theImage;
My logging shows that everything has the right length, yet theImage is always null.
I'm thinking it has to do with my content type. Or maybe the way I'm uploading?
Try this function. It works for me:
Bitmap loadPhotoBitmap(URL url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
FileOutputStream fos = new FileOutputStream("/sdcard/photo-tmp.jpg");
BufferedOutputStream bfs = new BufferedOutputStream(fos);
in = new BufferedInputStream(url.openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
bfs.write(data, 0, data.length);
bfs.flush();
BitmapFactory.Options opt = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not load file: " + this, e);
} finally {
closeStream(in);
closeStream(out)
closeStream(bfs);
}
return bitmap;
}