i'm tring to post base64 string to server and it won't post maybe because the sting is too long and i got this messege A connection to "server" was leaked. Did you forget to close a response body?
this is my code
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
selectedImageBitmap.recycle();
byte[] byteFormat = stream.toByteArray();
stream.close();
stream = null;
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.DEFAULT);
selectedImageBitmap=null;
System.gc();
byteFormat=null;
System.gc();
Log.d("image",""+ imgString);
Log.d("urlnya", "" + LOGIN_URL);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(LOGIN_URL);
HttpClient httpclientpost = new DefaultHttpClient();
final HttpResponse responsepost = httpclientpost.execute(post);
Log.d("respon post", "" + responsepost.getStatusLine().getStatusCode());
post.addHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("user_id", 6);
json.put("status", status_id);
json.put("keterangan", ket);
json.put("foto", imgString);
Log.d("image",""+ encodedImage);
StringEntity entity = new StringEntity(json.toString());
post.setEntity(entity);
HttpResponse responsepost2 = httpClient.execute(post);
Log.d("respon post2", "" + responsepost2.getStatusLine().getStatusCode());
// checking log for json response
Log.d("processing ", json.toString());
} catch (JSONException | IOException e) {
e.printStackTrace();
}
i've tried to post base64 which is with this long
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAQ4B4ADASIA
AhEBAxEB/8QAHwAAAAYDAQEBAAAAAAAAAAAABAUGBwgJAgMKAAEL/8QAfBAAAQMCBQIDBAcGAwQE
AAI7AQIDEQQFAAYSITEHQRNRYQgicYEJFDKRobHwFSNC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stL4ba2KlJI3IkydhxA5xGLNlgeo1AoXsdAI1BMEgR9kb7Tv
8sWN3vIr6g4E0hMIUBClHckRuEd9vh6YjpnTJFa2R/uDhPuQdJUP4ef3f8vLDdDhtIi1gbbx1m5I
mMXDKcwbJSkqAiBBIjl1M2/UHviCVQ0Q4tB/xArmQRtO08/r5Yzp6dXc8/P7UbfyHw9cOTmHLTtP
UvKVSrbWFpB2IiVK7FHn/bywTUdmcW5JZUQkp7EbyP8Al/Pv6YVU6TAmLR9+8X3mMXX8WktphUgB
IiRew6/Pc/vjVQ0igkGdikeU8nj4nCkp6UmJ9P8AKex/Pvg3Ysz2lOhg7Acg+vMJ+7v+ODJiiUgg
FEKBgg8yBsDInnDVTsbA/L079f264YrrAVQJJi4F+Ynnz++xMqimNMDm
or maybe longer i dont know exacly. but it won't post because it prosses too much string (maybe)
please help how can i post base64 to server. thank you
Related
I am using HttpClient 4.3.6 to perform http GET and POST requests. Right now I am using multipartentity to send a few string parameters and an image in the form of a file. I am able to successfully post the data but my problem comes in when I get the HTTP response. The response contains json data.
What happens is the HTTP response is incomplete and when i try to create a json object with the data i get jsonexception error saying:
Unterminated object at character 407.
I noticed that the response does not contain closed braces. Is this a problem on android or should I check the server? Because I am able to see the data properly on postman and on ios. I have never faced this issue before and don't know how to solve this.
This is my code to post and get the response:
#Override
protected String doInBackground(String... params) {
try {
String url = params[0];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(imageBytes, "image.jpg");
entity.addPart("image_data", bab);
entity.addPart("action", new StringBody("1", "text/plain", Charset.forName("UTF-8")));
entity.addPart("name", new StringBody("asdfg", "text/plain", Charset.forName("UTF-8")));
entity.addPart("user_id", new StringBody("157", "text/plain", Charset.forName("UTF-8")));
entity.addPart("birthday", new StringBody("18-04-1995", "text/plain", Charset.forName("UTF-8")));
entity.addPart("gender", new StringBody("male", "text/plain", Charset.forName("UTF-8")));
entity.addPart("is_jlpt_student", new StringBody(String.valueOf(0), "text/plain", Charset.forName("UTF-8")));
entity.addPart("relationship", new StringBody("Father", "text/plain", Charset.forName("UTF-8")));
entity.addPart("relationship_id", new StringBody(String.valueOf(10002), "text/plain", Charset.forName("UTF-8")));
entity.addPart("is_creator", new StringBody(String.valueOf(1), "text/plain", Charset.forName("UTF-8")));
entity.addPart("email", new StringBody(email, "text/plain", Charset.forName("UTF-8")));
httppost.setEntity(entity);
HttpResponse resp = httpclient.execute(httppost);
String response = EntityUtils.toString(resp.getEntity());
Log.i("HttpResponse", response);
return response;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute (String result) {
super.onPostExecute(result);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(result);
JSONObject json_data = jsonObject.getJSONObject("data");
String json_userid = json_data.getString("user_id");
String json_username = json_data.getString("name");
String json_email = json_data.getString("email");
String json_country = json_data.getString("country_code");
String json_imagefilename = json_data.getString("image_filename");
String json_imgurl = json_data.getString("image_url");
Toast.makeText(ParentGuardianProfile.this, "ImageFile " + json_imagefilename, Toast.LENGTH_SHORT).show();
User new_user = userdao.createUser(json_userid, json_username, json_email,json_imagefilename,json_country,selectedImageUri.toString(), 1);
Log.i("SQLITE", "added user : " + new_user.getmUserName() + new_user.getmId());
} catch (JSONException e) {
e.printStackTrace();
}
}
And my json response is :
{"status":1,"message":"success","data":{"child_id":"381","name":"asdfg","image_filename":"C201603021734476.jpg","image_url":"https:\/\/innokid.blob.core.windows.net\/media\/child\/381.jpg","birthday":"18-04-1995","gender":"male","is_jltp_student":"0","relationship":"Father","relationship_id":"10002","is_creator":1,"rank":1,"qrcode_url":"http:\/\/innokid.azurewebsites.net\/uploads\/qrcode\/child_381.png"
I tried using String buffer as suggested in this post String is being truncated when its too long . But i still get the same result.
Code looks ok at first glance.
How do you got know that the json data is cut? Logcat can truncate text. Debugger should be more reliable in this case.
Try to generate this same request with some tools like curl / SoapUI and validate JSON you got with some formatter / validator (you'll easily find a few of such tools).
It's beyond the range of question, but using raw Android built-in communication libraries seems to be a little bit masochistic. Have you ever consider to use Retrofit?
I think this code is problematic String response = EntityUtils.toString(resp.getEntity());
may be you should use some other function to convert response toString...
Apparently the json is missing two curly brackets '}}' at the end, which can happen due to some bug in the toString code.
I pulled up an old project that was using the org.apache.http stuff and below is how I was parsing the response. As you can see it is rather cumbersome. There are many tested and maintained libraries out there that are better suited to this kind of heavy-lifting.
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// Convert content stream to a String
resultString = convertStreamToString(instream);
instream.close();
// Do stuff with resultString here
// Consume Content
entity.consumeContent();
}
And the convertStreamToString() method:
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*
* (c) public domain:
* http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/
* 11/a-simple-restful-client-at-android/
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
I finally solved this issue by replacing httpclient library with Android Asynchronous Http Client. Now it works fine. Thanks a lot for your help!
However, I still dont understand why the response was truncated when i used httpclient.
I want to send a string data, "" , in bytes format to server in android app. I uses the HttpClient but i think its not the right way, please help me how to do this?
in case of .net , i want similar code in java.
string boundary = Guid.NewGuid().ToString();
HttpWebRequest request = HttpWebRequest.Create(url)
as HttpWebRequest;
request.Method = "POST";
//request.ContentType = "application/json";
request.PreAuthenticate = true;
byte[] fulldata = Encoding.UTF8.GetBytes(data);
request.ContentLength = fulldata.Length;
using (Stream sw = request.GetRequestStream())
{
sw.Write(fulldata, 0, fulldata.Length);
}
First convert your string data to byte and by using ByteArrayEntity send data to server in byte format.
try like this
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.1/xxx");
HttpResponse response;
HttpParams hp = new BasicHttpParams();
//use ByteArrayEntity to send string data in byteformat
ByteArrayEntity byteEntity = new ByteArrayEntity(byte_data);
httppost.setEntity(byteEntity);
response = httpclient.execute(httppost);
I am using JSON to get response from my server.
This is code:
HttpClient httpclient = new DefaultHttpClient();
HttpClient httpclient2 = new DefaultHttpClient();
HttpResponse response;
HttpResponse response2;
try {
HttpGet request = new HttpGet(GlobalConfig.getMagazineUrl());
HttpGet request2 = new HttpGet(GlobalConfig.getMagazinePagesUrl(1));
request.addHeader("Authorization", "Basic " + Base64.encodeToString(
(GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP));
request2.addHeader("Authorization", "Basic " + Base64.encodeToString(
(GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP));
response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
response2 = httpclient2.execute(request2);
StatusLine statusLine2 = response2.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK && statusLine2.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
response2.getEntity().writeTo(out2);
out.close();
out2.close();
return parser(out.toString(), out2.toString());
As you can see in parser(out.toString(), out2.toString()) I return both responses as String. I would like to know how I can merge this two JSON responses in one. I don't want to merge two strings, I need merge two JSON respons in one big response. This is possible? If yes how I can do that?
Perhaps thats what you want:
...
JSONObject json = new JSONObject();
json.put("response1", new JSONObject(out.toString()));
json.put("response2", new JSONObject(out2.toString()));
Now return either json.toString() or json depending on the return type.
I need to display an image in my android application. The problem is I want to get this image from somewhere online. The URL is like:
http://www.mydomain.com/hello world image.png
As you see the image name containing some spaces in it. Every time I execute my code this will show me exception of FileNotFound. and nothing happens.
Following is my code
String imagePathCon = "hello world image.png";
String imagePath = "http://www.mydomain.com/" + imagePathCon;
try {
URL url;
url = new URL(imagePath);
// url = new URL("http://www.azuma-kinba.com/wp-content/uploads/2012/05/Android-Make-Google-Loss-in-2010.png");
InputStream content = (InputStream)url.getContent();
Drawable d = Drawable.createFromStream(content , "src");
im.setImageDrawable(d);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I use to replace space with "+" but nothing happens.
This will work.
String imagePathCon = "hello world image.png";
imagePathCon=imagePathCon.replaceAll(" ", "%20");
String imagePath = "http://www.mydomain.com/" + imagePathCon;
You must know http://en.wikipedia.org/wiki/Percent-encoding#Character_data
plese use correct Url for getting image and use below code that would definetly help u...
replace space of URL using...
imagePath=imagePath.replaceAll(" ", "%20");
and now...
HttpGet httpRequest = new HttpGet(new URL(params[0]).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufHttpEntity.getContent();
//image_value = new URL("image Url is here");
bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
//imageLoader is object of iamge view
imageLoader.setImageBitmap(bm);
You need to url encode the path, like this:
String imagePathCon = URLEncoder.encode("hello world image.png", "UTF-8");
You can also use an http get request:
The simplest way is to simply call URLEncoder which will automatically replace all charaters in your string to the url encoded format.
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpGet = new HttpGet(URLEncoder.encode(url, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpGet);
in = httpResponse.getEntity().getContent();
//Bitmap bmp = BitmapFactory.decodeStream(instream);
//create a bitmap or an image from the input stream
I've been trying this for the best part of two weeks now, and I am really stuck. Initially I had created a simple ObjectOutputStream client - server program - with the client being the Android app, but it does not work (it reads the connection but not the object).
So now I am confused as to what other approaches I might be able to take to carry out this simple task? Can anyone Help?
have you tried URLConnection using post method? :)
Or get method like:
String yourURL = "www.yourwebserver.com?value1=one&value2=two";
URL url = new URL(yourURL);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
response = in.readLine();
you can try JSON stirng to send data. We have a lot of stuff available on how to work with JSON and also there are many api's. JSONSimple is the one I can suggest. Its really easy.
why don't you try this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
You can use this to post an Entity to server:
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
postRequest.setEntity(entity);
try {
HttpResponse response = httpClient.execute(postRequest
);
String jsonString = EntityUtils.toString(response
.getEntity());
Log.v(ProgramConstants.TAG, "after uploading file "
+ jsonString);
return jsonString;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
An Entity can be name value pair:
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("key1", value1));
nvps.add(new BasicNameValuePair("key2", value2));
Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8)
Or you can send an entity with bytearray.
Bitmap bitmapOrg=getBitmapResource();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();
MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart("file", new ByteArrayBody(data, "image/jpeg",
"file"));
If you want to post json to server:
Please check out this link How do I send JSon as BODY In a POST request to server from an Android application?
For serializing and deserializing java object, I recommend https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson
Really hope it can help you see an overview of sending data to server