所以我对编码还很陌生,我正在尝试为我的机器人团队制作一个侦察应用程序。我正在使用蓝色联盟的 API,现在我试图一次仅从某个 api 返回一条数据。
我已经得到了它,这样当按下按钮时我就可以拿回一件(城市),但现在我想添加第二个按钮来拿回其他东西。我该怎么办?
我再次对此感到非常陌生。
提前谢谢您 这才是行之有效的那一位。我必须评论第二个返回,因为它不起作用
import com.thebluealliance.api.v3.models.Team;
import java.io.IOException;
public class TBAAsyncGetter extends AsyncTask<TBA, Void, String> {
@Override
protected String doInBackground(TBA... tbas) {
try {
Event[] ourEvents = tbas[0].teamRequest.getEvents(5496);
Event event = ourEvents[0];
return ourEvents[0].event_code;
//return ourEvents[0].event_code;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
这些是按钮等。 (忽略所有评论)
package com.example.scoutingapp4;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
import com.thebluealliance.api.v3.TBA;
import java.util.concurrent.ExecutionException;
public class OurTeamFragment extends Fragment implements View.OnClickListener {
Gson gson = new Gson();
String authKey = "tatIjlJ60oDWTv9aDk1ZDTL5GM1IJyafzPeJpWk4dR5adIgsdJt3sXvimEHA3MrI";
TBA tba = new TBA(authKey);
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_ourteam, container, false);
}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btnGet = (Button) getView().findViewById(R.id.btnRegional);
assert btnGet != null;
btnGet.setOnClickListener(this);
Button btnGet2 = (Button) getView().findViewById(R.id.btnEventCode);
assert btnGet2 != null;
btnGet2.setOnClickListener(this);
}
public void onClick(final View v) {
v.setEnabled(false);
switch (v.getId()) {
case R.id.btnRegional:
try {
TextView textHeyo = (TextView) getView().findViewById(R.id.text_regional);
textHeyo.setText(new TBAAsyncGetter().execute(tba).get());
} catch ( ExecutionException | InterruptedException e) {
e.printStackTrace();
}
// AsyncHttpClient client = new AsyncHttpClient();
// client.addHeader("X-TBA-Auth-Key", "tatIjlJ60oDWTv9aDk1ZDTL5GM1IJyafzPeJpWk4dR5adIgsdJt3sXvimEHA3MrI");
// client.get("https://www.thebluealliance.com/api/v3/team/frc5496/events/2019", new AsyncHttpResponseHandler() {
// @Override
// public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// if (responseBody != null) {
// TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
// assert textHeyo != null;
// gson.toJson(new String(responseBody));
//
// System.out.println("body=" + new String(responseBody));
// textHeyo.setText(new String(responseBody));
//
// }
// v.setEnabled(true);
// }
//
// @Override
// public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
// TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
// textHeyo.setText(new String("Boom! ") + new String(responseBody));
//
// v.setEnabled(true);
// }
//
// });
break;
case R.id.btnEventCode:
try {
TextView textHeyo = (TextView) getView().findViewById(R.id.text_event_code);
textHeyo.setText(new TBAAsyncGetter().execute(tba).get());
} catch ( ExecutionException | InterruptedException e) {
e.printStackTrace();
}
// AsyncHttpClient client2 = new AsyncHttpClient();
// client2.addHeader("X-TBA-Auth-Key", "tatIjlJ60oDWTv9aDk1ZDTL5GM1IJyafzPeJpWk4dR5adIgsdJt3sXvimEHA3MrI");
// client2.get("https://www.thebluealliance.com/api/v3/event/2020cadm/teams", new AsyncHttpResponseHandler() {
// @Override
// public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// if (responseBody != null) {
// TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
// assert textHeyo != null;
// System.out.println("body=" + new String(responseBody));
// textHeyo.setText(new String(responseBody));
// }
// v.setEnabled(true);
// }
//
// @Override
// public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
// TextView textHeyo = (TextView) getView().findViewById(R.id.text_view_result);
// textHeyo.setText(new String("Boom! ") + new String(responseBody));
//
// v.setEnabled(true);
// }
//
// });
break;
}
}
}
最佳答案
看起来,按照您的设置方式,您可以将其更改为接受 TBAAsyncGetter 类的 doInBackground 方法上的附加参数,您还可以在其中传递一些内容来指定您想要返回的值。提到的另一种方法是将返回类型从 String 更改为您创建的保存信息的对象,或者甚至只是返回整个 Event 数组信息并在 OurTeamFragment 类中挑选出您需要的内容。
另一种方法是调用两个单独的 doInBackground 方法,一个用于您尝试返回的每组数据,并且每次只需调用您需要的方法。例如,这可能是 doInBackgroundGetCity 和 doInBackgroundGetOther,并且每个都经过硬编码以返回该特定值。这不是最干净的解决方案,但这些可能会给您带来正确的方向,帮助您解决这个问题。
关于java - 如何使用 try catch 从字符串中获取两条数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60288841/