Jason, you're right about the efficiency of appending to a string. I
use it for short messages only. I use an input stream to create a DOM
document, which probably isn't efficient either. But then, if all you
get is 200 bytes at most, who cares.
Christine
On Oct 26, 11:06 pm, Jason Proctor <[EMAIL PROTECTED]> wrote:
> try logging the exception itself in the error cases. just printing
> "IOE" will get the fact that an IOException happened, but not any
> detail.
>
> my code is very similar to yours. HttpResponse = HttpClient.execute
> (HttpGet), InputStream = HttpResponse.getEntity ().getContent ().
> then after that i start munching on the stream.
>
> btw appending to a String each time is massively inefficient. the
> right way, IMHO, to buffer a resource as a String is to use an
> InputStreamReader whose products are routed to a StringWriter. use a
> suitable buffer size -- i use 1024 chars.
>
> hth
> j
>
> >The first one still gives me an IOException. It forces me to add
> >throws or a couple of try/catch's for error handling but either way it
> >gets IOException. This is the code for that:
>
> >public String getPrice(String symbol) throws ClientProtocolException,
> >IOException {
> > DefaultHttpClient client = new DefaultHttpClient();
> > HttpGet get = null;
> > get = new HttpGet("http://oxavi.com/price.php");
>
> > String buffer = null;
> > HttpResponse response = client.execute(get);
> > HttpEntity entity = response.getEntity();
> > int status = response.getStatusLine().getStatusCode();
> > InputStream in = entity.getContent();
> > for (int i = in.read(); i != -1; i = in.read()) {
> > buffer += (char) i;
> > }
> > in.close();
> > if (entity != null) {
> > entity.consumeContent();
> > }
>
> > return buffer;
> >}
>
> >When I run it I do it like this:
>
> >String newprice;
> > try {
> > newprice =
> >getPrice(DbAdapter.KEY_SYMBOL);
> > } catch (ClientProtocolException e) {
> > newprice = "CPE";
> > } catch (IOException e) {
> > newprice = "IOE";
> > }
>
> >Which results in a newprice of IOE.
>
> >The second code also doesnt work for me. I had to change alot to get
> >rid of the errors, so here is mine:
>
> >public String getPrice(String symbol) {
> > DefaultHttpClient client = new DefaultHttpClient();
> > HttpGet get = null;
> > get = new HttpGet("http://oxavi.com/price.php");
>
> > HttpResponse response = client.execute(get);
> > HttpEntity entity = response.getEntity();
> > int status = response.getStatusLine().getStatusCode();
> > InputStream in = entity.getContent();
> > Document document = getDocument(in);
> > in.close();
> > if (entity != null) {
> > entity.consumeContent();
> > }
>
> >}
>
> >The only problem is that it does not recognize getDocument(), it tells
> >me to create a method for it. And I also don't understand how to
> >return a string from all of this, do I have to convert the document to
> >one after it gets it?
>
> >On Oct 26, 1:52 pm, Christine <[EMAIL PROTECTED]> wrote:
> >> this works:
> >> String buffer = null;
> >> HttpResponse response = client.execute(get);
> >> entity = response.getEntity();
> >> status = response.getStatusLine().getStatusCode();
> >> InputStream in = entity.getContent();
> >> for (int i = in.read(); i != -1; i = in.read()) {
> >> buffer += (char) i;
> >> }
> >> in.close();
> >> if (entity != null) {
> >> entity.consumeContent();
> >> }
>
> >> or this
> >> HttpResponse response = client.execute(get);
> >> entity = response.getEntity();
> >> status = response.getStatusLine().getStatusCode();
> >> InputStream in = entity.getContent();
> >> Document document = getDocument(in);
> >> in.close();
> >> if (entity != null) {
> >> entity.consumeContent();
> >> }
>
> >> Christine
>
> >> On Oct 26, 10:05 am, JamesB <[EMAIL PROTECTED]> wrote:
>
> >> > I have been trying to get the content of a web page which just has
> >> > only a string on it. I want to put the entire content of the page
> >> > into a variable so I can use it. I found some code in another thread
> >> > and tried to use it, but it isnt working for me.
>
> >> > public String getPrice(String symbol) {
> >> > DefaultHttpClient client = new DefaultHttpClient();
> >> > HttpGet get = null;
> >> > HttpResponse resp = null;
>
> >> > get = new HttpGet("http://oxavi.com/price.php");
>
> >> > try {
> >> > resp = client.execute(get);
> >> > } catch (ClientProtocolException e) {
> >> > return "resp ParseException e";
> >> > } catch (IOException e) {
> >> > return "resp IOException e";
> >> > }
>
> >> > HttpEntity entity = resp.getEntity();
>
> >> > try {
> >> > String price = EntityUtils.toString(entity);
> >> > return price;
> >> > } catch (ParseException e) {
> >> > return "EntityUtils ParseException e";
> >> > } catch (IOException e) {
> >> > return "EntityUtils IOException e";
> >> > }
> >> > }
>
> >> > When I run it the function returns 'resp IOException'. I want it to
> >> > return '234.52', the entire content of the page.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---