如何在Google Glass GDK上发出HTTP GET请求

时间:2016-01-01 20:57:57

标签: http http-get google-gdk

我有一些代码,但new HTTPRequest().execute();无效。此代码应该做的是使用HTTP GET请求,然后在屏幕上显示文本(使用代码行card.setText(mResult);)。我的整个HTTPRequest类可能都错了?是否有一个存在的类我可以打电话来说像ExampleHTTPGetClass.getURLData("example.com");我对玻璃开发相当新,这是我试写的代码:

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

谢谢,瑞安。

1 个答案:

答案 0 :(得分:2)

在执行Http请求之前调用buildView()和setText()。在AsyncTask和setText中添加onPostExecute。试试这个:

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

另外,不要忘记添加

<uses-permission android:name="android.permission.INTERNET"/> 
清单中的