gpt4 book ai didi

android - mapsActivity异步方法获取JSON

转载 作者:行者123 更新时间:2023-11-29 23:16:54 24 4
gpt4 key购买 nike

我创建了一个基本的 mapsActivity,我想从 JSON 字符串中获取坐标。我用于从服务器获取 JSON 字符串的方法是异步的,因此当我使用调试工具时我的 JSON 字符串显示为空。

我看到 onActivityResult 可以帮助解决我的问题,但是当我键入“@Override”时,它说“方法不覆盖其父类(super class)中的方法”。我应该怎么做才能使用 MapsActivity 中的数据获取我的 JSON 字符串?

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

String json_string; //ici seront stockées les information concernant les établissements

protected static String JSON_STRING;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK){
json_string = data.getStringExtra("JSON_string");
}
}

@Override
public void onMapReady(GoogleMap googleMap) {



Intent myIntent = new Intent(this, getJSON.class);
startActivityForResult(myIntent, 1);


/*
LatLng flams = new LatLng(47.221626, -1.557679);
mMap.addMarker(new MarkerOptions().position(flams).title("Le flams"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(flams));*/
}

getJSON.java

public class getJSON extends AppCompatActivity {

String json_string;
protected static String JSON_STRING; //string for both getJSON and mapsActivity

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

new BackgroundTask().execute();

}


public class BackgroundTask extends AsyncTask<Void, Void, String> {


String json_url = "the URL of my server";

@Override
protected String doInBackground(Void... voids) {

try {
URL url = new URL(json_url);
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();

while ((json_string = bufferedReader.readLine()) != null){
stringBuilder.append(json_string+"\n");
}

bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim(); <= while this shows the data

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

public BackgroundTask() {
super();
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String result) {
JSON_STRING=result;
Intent intent = new Intent();
intent.putExtra("JSON_string", JSON_STRING);
setResult(RESULT_OK, intent);
finish();
}

最佳答案

根据您的代码,当您启动 Activity getJSON 时,从服务器端获取数据需要时间,这就是您无法立即获取 json_string 的值的原因。顺便说一句,请按照我的指导正确制作逻辑。

当一个Activity startActivityForResult 然后会在onActivityResult 方法中得到结果。

在您的代码中,当您启动 Activity getJSON 时,需要将 JSON_STRING 发送回 mapsActivity,如下代码所示:

   Intent intent = new Intent();
intent.putExtra("JSON_string", JSON_STRING);
setResult(RESULT_OK, intent);
finish();

您将在 mapsActivity 中的方法 onActivityResult 中获得结果:

   @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK){
json_string = data.getStringExtra("JSON_string");
}
}

有什么不明白的可以问我。

关于android - mapsActivity异步方法获取JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167796/

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