gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:07:35 25 4
gpt4 key购买 nike

我有一些代码,但是 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;
}
}
}

谢谢,瑞安。

最佳答案

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

在 list 中。

关于http - 如何在 Google Glass GDK 上发出 HTTP GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34559983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com