tags:

views:

1090

answers:

4
package com.example.helloandroid;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.util.EntityUtils;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try
        {
            HttpClient hc = new DefaultHttpClient();
            HttpGet get = new HttpGet("http://www.yahoo.com");
            HttpResponse rp = hc.execute(get);

            if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                //String str = EntityUtils.toString(rp.getEntity());
                InputStream is = rp.getEntity().getContent();           
                String str = is.toString();

                TextView tv = new TextView(this);
                tv.setText(str);
                setContentView(tv);

            }
            else
            {
                System.out.println("halo,baby.");
            }
        }catch(IOException e){
            return;
        }
    }
}

the above is my code.
but it seems there are some problems here.
So, Anyone can give me some ideas on it?
Thank you.

Carmen Lau

A: 

Without knowing what the problem is it's hard to diagnose, but I suspect the following line isn't what you want:

String str = is.toString();

Object.toString() returns "a string containing a concise, human-readable description of this object." Which is not always what you'd expect. You'll probably actually want to pull the data out by hand with something like:

byte[] readBytes; // create an empty byte array
is.read(readBytes); // read from the InputStream and put input in the byte array
String str = new String(readBytes); // creat a new String from the byte array

Then set the text on the TextView to str like you were already doing.

fiXedd
A: 

For mldj and MattC, The problem is I cannot print out the response message. As my coding, the import parts also post out, but I don't know why they become in Black letters only in the beginning of my coding.

Actually, I want to send a get request to Yahoo and get back the response and print it out. but now I open my app and it is blank.

So, I am not sure the problem is (1) I using the wrong method to print it out OR (2) my app cannot send the request to Yahoo and haven't get back any response for me from the beginning to now on.

For fiXedd, I will try your suggested code tonight. I think it can solve the problem if I really use a wrong method for print out the response message. Thank you very much.

A: 

Actually, I had get something back now. but the content like the source code of Yahoo. So, is it correct?

A: 

Hello,

I'm trying to do the same thing as Carmen (I'm copy-pasting her code), but I'm not getting any results even after fiXedd's suggestions. The only thing different I have in my code is the initialization of "readBytes":

byte[] readBytes = null;

I don't know why am I just getting an empty screen in the emulator. I set up the permissions in the Manifest for Android to connect to Internet. I'm also not getting the error message "halo baby".

Carmen, I'm not planning on stealing your work, hehe. This is a test exercise for a bigger project I'll have to work on and I'm still very new with Java.

Please help me! Thank you very much for the help so far.

JaySS

JaySS