getJsonStringFromURL() return a null string - java

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.

Related

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());

how to convert string data into json?

here string data values comes from shell script code.i need to show this resultant values in json format
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[]{"/home/admin/institutestatus.sh"});
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
Gson gson =new Gson();
System.out.println(gson.toJson(line));
}
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
JSONArray arr = new JSONArray();
while ((line = input.readLine()) != null) {
JSONObject obj = new JSONObject();
obj.put("value",line);
arr.add(obj);
}
I've used this function to convert InputStream to String and the to JSONObject.
public static String load(final InputStream in) {
String data = "";
try {
InputStreamReader is = new InputStreamReader(in, Charset.forName("UTF-8"));
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while (read != null) {
sb.append(read);
read = br.readLine();
}
data = sb.toString();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return data;
}
You use the GSON in order to don't have to work with the Strings in the first place...

Read url content into a String

I am reading the contents of buffered reader in the below method:
public static String readBuffer(Reader reader, int limit) throws IOException
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < limit; i++) {
int c = reader.read();
if (c == -1) {
return ((sb.length() > 0) ? sb.toString() : null);
}
if (((char) c == '\n') || ((char) c == '\r')) {
break;
}
sb.append((char) c);
}
return sb.toString();
}
I am invoking this method later to test -
URL url = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer sb = new StringBuffer();
String line=null;
while((line=readBuffer(in, 2048))!=null) {
sb.append(line);
}
System.out.println(sb.toString());
My question here is, I am returning the contents of bufferedreader into a string in my first method, and appending these String contents into a StringBuffer again in the second method, and reading out of it. Is this the right way? Any other way I can read the String contents that has contents from url?Please advise.
I hope this works -
public static String readFromURL(){
URL url = new URL("http://www.oracle.com/");
StringBuilder responseBuilder = new StringBuilder();
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
int resCode = httpCon.getResponseCode();
InputStream is = null;
if (resCode == 200) {
is = httpCon.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String response = null;
while (true) {
response = reader.readLine();
if (response == null)
break;
responseBuilder.append(response);
}
}
return responseBuilder.toString();
}

Transfering Base64 String over Http REST service with JSON response

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();
}

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