httphttp-getgoogle-gdk

How to make a HTTP GET request on Google Glass GDK


I have some code, but the new HTTPRequest().execute(); won't work. What this code should do is use an HTTP GET request and then display the text on the screen (with the line of code card.setText(mResult);). My entire HTTPRequest class may be wrong? Is there a class that exists that I can call to say something like ExampleHTTPGetClass.getURLData("example.com"); I am fairly new to glass development, here is my code I tried to write:

public class MainActivity extends Activity {

    private CardScrollView mCardScroller;
    private View mView;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        mView = buildView();

        mCardScroller = new CardScrollView(this);
        mCardScroller.setAdapter(new CardScrollAdapter() {
            @Override
            public int getCount() {
                return 1;
            }

            @Override
            public Object getItem(int position) {
                return mView;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                return mView;
            }

            @Override
            public int getPosition(Object item) {
                if (mView.equals(item)) {
                    return 0;
                }
                return AdapterView.INVALID_POSITION;
            }
        });
        // Handle the TAP event.
        mCardScroller.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Plays disallowed sound to indicate that TAP actions are not supported.
                AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                am.playSoundEffect(Sounds.DISALLOWED);
            }
        });
        setContentView(mCardScroller);

        new HTTPRequest().execute();

    }

    private View buildView() {
        CardBuilder card = new CardBuilder(this, CardBuilder.Layout.TEXT);

        card.setText(mResult);
        return card.getView();
    }

    HttpURLConnection mURLConnection;
    String mResult;

    private class HTTPRequest extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... arg0){
            try{
                URL url = new URL("http://exampleURL.com");
                mURLConnection = (HttpURLConnection)
                        url.openConnection();
                InputStream in = new BufferedInputStream(
                        mURLConnection.getInputStream());
                int ch;
                StringBuffer b = new StringBuffer();
                while ((ch = in.read()) != -1){
                    b.append((char) ch);
                }

                mResult = new String(b);
                Log.d("App", mResult);

            } catch (Exception e){}
            return null;
        }
    }
}

Thanks, Ryan.


Solution

  • You call buildView() and setText() before executing the Http request. Add onPostExecute in your AsyncTask and setText there. Try this:

    private class HTTPRequest extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... arg0){
            try{
                URL url = new URL("http://exampleURL.com");
                mURLConnection = (HttpURLConnection)
                        url.openConnection();
                InputStream in = new BufferedInputStream(
                        mURLConnection.getInputStream());
                int ch;
                StringBuffer b = new StringBuffer();
                while ((ch = in.read()) != -1){
                    b.append((char) ch);
                }
    
                mResult = new String(b);
                Log.d("App", mResult);
    
            } catch (Exception e){}
            return null;
        }
        @Override
        protected void onPostExecute(Void v) {
            mView = buildView();
        }
    }
    

    Also, don't forget to add

    <uses-permission android:name="android.permission.INTERNET"/> 
    

    in the manifest.