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

How to make a HTTP GET request on Google Glass GDK

我有一些代码,但是 new HTTPRequest().execute(); 不起作用。这段代码应该做的是使用 HTTP GET 请求,然后在屏幕上显示文本(代码行 card.setText(mResult);)。我的整个 HTTPRequest class 可能是错误的?是否存在 class 我可以打电话说类似 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;
        }
    }
}

谢谢,瑞安。

您在执行 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"/> 

在清单中。