?
Solved

parse response by line feed

Posted on 2005-04-26
14
Medium Priority
?
251 Views
Last Modified: 2012-06-22
hi
 can someone help me how to modify this code to do the following:

i need to parse the response to split it by line feed and the pass each line in a loop to the uploadReasponse method...meaning the response is a huge csv response each line delimited by a line feed..thanks

HttpClient httpClient = new HttpClient();
NameValuePair data = new NameValuePair("txtBatchID", vbatchID);
authpost.setRequestBody(
new NameValuePair[] {data});
int responseCode = httpClient.executeMethod(authpost);
String responseBody = authpost.getResponseBodyAsString();
uploadResponse(responseBody,conn);
authpost.releaseConnection();
0
Comment
Question by:desiboy1974
  • 7
  • 3
  • 3
  • +1
14 Comments
 

Author Comment

by:desiboy1974
ID: 13872782
anyone?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13872991
Do you mean (calling uploadResponse for each line inside responseBody)?

String responseBody = authpost.getResponseBodyAsString();
StringTokenizer stTokens = new StringTokenizer(responseBody, "\n");
while (stTokens.hasMoreTokens())
uploadResponse(stTokens.nextToken(),conn);
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13873944
String[] lines = responseBody.split("\\n");
for (int i=0; i<lines.length; i++)
{
   uploadResponse(lines[i], conn);
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13874030
The following is probably more efficient and also getResponseBodyAsString, while not technically deprecated, is discouraged:

String buffer = null;
BufferedReader in = new BufferedReader(new InputStreamReader(authpost.getResponseBodyAsStream()));
while ((buffer = in.readLine()) != null) {
      // got it
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13878151
desiboy1974,
If you don't mind to use authpost.getResponseBodyAsStream() and you don't need the response as a string then CEHJ is probably your best option (both memory and cpu wise).
Though you might want to call it this way (when converting stream to reader):
BufferedReader in = new BufferedReader(new InputStreamReader(authpost.getResponseBodyAsStream(), authpost.getResponseCharSet()));

The suggestion by objects requires JDK 1.5 (may not be an issue for you) and most likely will have the worst performance (both memory and cpu).
0
 
LVL 92

Expert Comment

by:objects
ID: 13880831
> The suggestion by objects requires JDK 1.5

Why???
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13880896
>>>>  The suggestion by objects requires JDK 1.5
>> Why???
I meant 1.4 (sorry for that).
0
 
LVL 92

Expert Comment

by:objects
ID: 13880934
And I doubt the performance is any worse as well :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13881063
>> And I doubt the performance is any worse as well :)
I am willing to bet on that.
1. You are traversing the buffer twice.
2. You are creating many more objects before you start using them.
3. The difference is similar conceptually to the difference between SAX and DOM (work while you parse versus parse all and then work on the parsed structure).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13881089
>>... and you don't need the response as a string then CEHJ is probably your best option (both memory and cpu wise).

Even if you do need it as a String, you'll still get more control that way, say by reading into a StringBuffer
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13881216
>> Even if you do need it as a String, you'll still get more control that way, say by reading into a StringBuffer
Maybe, if such control is needed. Though at that point the API for getResponseBodyAsString will be more efficient for creating such string
as they are creating the string by wrapping the internal byte array.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13881499
These are the api notes for getResponseBodyAsString:

"Note: This will cause the entire response body to be buffered in memory. A malicious server may easily exhaust all the VM memory. It is strongly recommended, to use getResponseAsStream if the content length of the response is unknown or resonably [sic] large."

>>Though at that point the API for getResponseBodyAsString will be more efficient ...

No reason why it should be - they're only reading from the stream into a buffer, which can be done in the app just the same


0
 
LVL 15

Expert Comment

by:aozarov
ID: 13881653
>> These are the api notes for getResponseBodyAsString:
As I said before, if you don't need the buffer as a string then getting the inputstream, as you suggested, is a better aproach.

>> No reason why it should be
Looking at the source code it seems that the body is stored as byte array.
So when you call getResponseBodyAsString they wraps the byte array with a String
And when you call getResponseBodyAsStream they wraps the byte array with ByteInputStream.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13882507
desiboy1974,
I think you picked the worst option.
Good luck.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question