Transfering Base64 String over Http REST service with JSON response - java

I have a webservice that returnds a json response , the json response contains both plain text and base64 encoded images , I am consuming that service using android app so I implemented progress bar to indicate the progress .
Implementing progress bar forces me to use BufferedInputStream to read the response and update the progress based on what the app is reading .
The problem is that everything is working fine and the progress is updating correctly, but after collecting the response and exiting the while loop , I try to convert the string into json format using JSONObject.
Here is the code snippet
BufferedInputStream bis = new BufferedInputStream(responseEntity.getContent());
StringBuilder sb = new StringBuilder();
String line = null;
int total = 0 ;
int count = 0 ;
byte[] buffer = new byte[4096];
StringBuffer sBuffer = new StringBuffer();
StringWriter sw = new StringWriter();
String content = new String();
while((count = bis.read(buffer)) > 0){
content += new String(buffer,Charset.defaultCharset());
total += count;
publishProgress(""+(int )total*100/this.contentSize);
Log.i("updating",""+(int )total*100/this.contentSize);
}
bis.close();
// String content = new String(sb);
// Log.i("ServerRawresponse",content);
try {
Log.i("REsponse_Content",content.replaceAll("\"", ""));
responseString = new JSONObject(new JSONTokener(content.replaceAll("\"", "\\\"")));
//System.out.println(content);
} catch (JSONException e) {
e.printStackTrace();
}
Any help please

Try this methods works perfectly with me
HttpResponse WSresponse = httpclient.execute(httppost);
String response = getResponseBody(WSresponse.getEntity());
JSONObject jobj = new JSONObject(response);
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
System.out.println("GEN START : " + Calendar.getInstance().getTimeInMillis());
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
return buffer.toString();
}

Related

JSONTokener: A JSONObject text must begin with '{'

Hitting exception in my code below for using JSON on a URI
public static String processRestResponse(String language){
URI uri = null;
JSONTokener tokener = null;
try {
uri = new URI("https://api.github.com/search/repositories?q=language:Java");
URL url = uri.toURL();
InputStream inputStream = url.openStream();
tokener = new JSONTokener(inputStream.toString());
JSONObject root = new JSONObject(tokener);
} catch (Exception e) {
e.printStackTrace();
}
Exception as follows...
org.json.JSONException: A JSONObject text must begin with '{' at character 1
at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
at org.json.JSONObject.<init>(JSONObject.java:179)
at Assignment1.processRestResponse(Assignment1.java:48)
at Assignment1.main(Assignment1.java:108)
Is there an alternative approach i can take that would suit?
make sure you read the the InputStream correctly using the right charset, for example like this:
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
}
afteron you can use it:
tokener = new JSONTokener(textBuilder.toString());
JSONObject root = new JSONObject(tokener);

Losing file data calling openFileInput on my service eventually as the service runs on Android

On my service, I want to check data every few seconds.
This works for a few minutes, but eventually subs.txt becomes an empty file and the previously entered data is lost.
while(true) {
SystemClock.sleep(1000);
try {
//looking if needed to update
FileInputStream fin = openFileInput("subs.txt");
String objEnc = "";
BufferedReader input = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
StringBuilder sbuilder = new StringBuilder();
objEnc = input.readLine();
while (objEnc != null) {
sbuilder.append(objEnc);
objEnc = input.readLine();
if (objEnc != null) {
// sbuilder.append("\n");
}
}
input.close();
objEnc=sbuilder.toString();
fin.close();
Gson gson = new Gson();
ManySubs obj = gson.fromJson(objEnc, ManySubs.class);
catch(Exception e){
}
}

StringBuilder lost data when convert to String

here what's the problem
I have problem when I tried to get String from my StringBuilder
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 128 * 1024);
StringBuilder dataResponseSB = new StringBuilder();
String line ;
while ((line = reader.readLine()) != null) {
dataResponseSB.append(line);
if (DataFactory.DEBUG_MODE) {
// all data here are complete
Log.i("===LoadDataActivity","line: "+line);
}
}
String rawdata = new String(dataResponseSB); // dataResponseSB.toString(); also not work
if (DataFactory.DEBUG_MODE) {
// data here are lost
Log.i("===LoadDataActivity","rawdata: "+rawdata);
}
(-) I receive a huge data from BufferedReader .readLine()
(-) I use Log to check and sure that I got about 5 line of 8000 Buffer Size per line and I am very sure that I have receive all data properly
(1) I append each line to StringBuilder Here
(-) after I append all the line to StringBuilder
(2) I try to convert it back to String
(-) Now, the problem, the when I check to new String here, the data have only 8192 (it should contain at least 30,000 or more)
What is the problem ? I am not sure it lost when it append to StringBuilder(1) or it lost when it convert back to String (2)
I add the code that I have tried below here ,, I have tried both UTF8 and without UTF8
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
//params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, );
params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 128 * 1024);
HttpClient client = new DefaultHttpClient(params);
// HttpClient client = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(DataFactory.REQUEST_API_URL + "?id=" + DataFactory.USER_ID );
// Depends on your web service
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpConnectionParams.setSocketBufferSize(client.getParams(), 128 * 1024);
HttpResponse response = client.execute(httppost);
//response.setParams(client.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 128 * 1024));
//String rawdata = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
// String rawdata = EntityUtils.toString(response.getEntity());
String rawdata = getResponseBody(response.getEntity());
//Scanner s = new Scanner(response.getEntity().getContent()).useDelimiter("\\A");
//String rawdata = s.hasNext() ? s.next() : "";
/*
//BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// ===================
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 128 * 1024);
StringBuilder dataResponseSB = new StringBuilder();
String line ;
while ((line = reader.readLine()) != null) {
dataResponseSB.append(line);
if (DataFactory.DEBUG_MODE) {
Log.i("===LoadDataActivity","line: "+line);
}
}
dataResponseSB.trimToSize();
String rawdata = new String(dataResponseSB);
/*
InputStreamReader reader = new InputStreamReader(response.getEntity().getContent());
StringBuffer sb = new StringBuffer();
int c;
while ((c = reader.read()) != -1) {
sb.append((char)c);
if (DataFactory.DEBUG_MODE) {
//Log.i("===LoadDataActivity","line: "+line);
}
}
*/
I'm pretty sure this is the problem:
Log.i("===LoadDataActivity","rawdata: "+rawdata);
You're assuming that a log entry can include all of your data - I believe each log entry is limited to 8192 characters.
I suggest you log rawdata.length() and you'll see that it's actually got all of the data - it's just logging it that's failing.
try this,,
public String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
System.out.println("GEN END : " + Calendar.getInstance().getTimeInMillis());
return buffer.toString();
}
// Try this way,hope this will help you to solve your problem...
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
System.out.println("Buffer : " + buffer.toString());

getJsonStringFromURL() return a null string

I'm trying to get a json string from a url and my method is returning a null string value when I use this line of code:
String jsonStr = getJsonStringFromURL(url);
Here is the method I'm using:
public static String getJsonStringFromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jsonObject = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e) {
return null;
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e) {
return null;
}
return result;
}
I have a url variable used where when I copy and paste the url into a browser it does return and display a json string. Any suggestions or help would be greatly appreciated.
The thing is you assigning value to the result when BufferReader is closed. Thats why you getting the null value.
Instead of assigning result = sb.toString(); outside of the BufferReader assign it before closing it.
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
System.out.println(result);// It will print you the value
is.close();
Hope it helps.

Android JSON receive issue

I have a string problem, I used the below code for recieving JSON data from an URL, the code is working fine, but the problem is I am not getting full data only half of the JSON values are coming, I would like to know whether there is reason for this, if so means how to solve this problem. JSON string is very big
DefaultHttpClient http_client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse response = http_client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
int n = in.read(b);
while(n>0){
out.append(new String(b, 0, n));
n = in.read(b);
}
String resultdata = out.toString();
Log.d("Out data",resultdata);
Try getting data like this using BufferedReader
String line="";
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
// Read response
while ((line = rd.readLine()) != null) {
total.append(line);
}
String jsonString=total.toString();
InputStream is = entity.getContent();
BufferedReader out = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = out.readLine()) != null) {sb.append(line + "\n");
}
is.close();
String json = sb.toString();
try this
Please try this,
public static JSONObject getJson(String url){
InputStream is = null;
String result = "";
JSONObject jsonObject = null;
// HTTP
try {
HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch(Exception e) {
return null;
}
// Read response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch(Exception e) {
return null;
}
// Convert string to object
try {
jsonObject = new JSONObject(result);
} catch(JSONException e) {
return null;
}
return jsonObject;
}}
you have to remove this line
int n = in.read(b);
and add
int n=0;
while ((n= in.read(b)) != null)

Categories