我有一个异步任务,从URL中获取JSON数组,然后将其解析为String。
我的问题是我无法将获取的数据从URL移动到我的Google Maps活动(地图活动)。当我在Asynctask中尝试Intent时,我无法做到。
请帮忙!!提前致谢
异步任务:
package com.example.panos.fleet_management;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import static android.app.PendingIntent.getActivity;
import static android.content.ContentValues.TAG;
import static android.support.v4.content.ContextCompat.startActivity;
/**
* Created by panos on 18/8/2018.
*/
public class MapRequest extends AsyncTask<Void,Void, Void> {
String data="";
String dataParsed="";
String singleParsed="";
double lon[],lat[];
String reg;
private Context context;
public MapRequest(Context context){
this.context=context;
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url= new URL("xxx.xxx");
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream= httpURLConnection.getInputStream() ;
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line= "";
while(line != null) {
line = bufferedReader.readLine();
data=data + line;
}
JSONArray JA=new JSONArray(data);
for (int i=0;i<JA.length();i++){
JSONObject JO= (JSONObject) JA.get(i);
singleParsed="Registration:"+ JO.get("Registration") + "\n"+
"Longitude:"+ JO.get("Longitude") + "\n"+
"Latitude:"+ JO.get("Latitude") + "\n";
dataParsed= dataParsed + singleParsed + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent(context, MapActivity.class);
intent.putExtra("dat",dataParsed);
}
}
JSON文件
[{
"Registration": "YHB6595",
"Longitude": "11.00000000",
"Latitude": "3.00000000"
}, {
"Registration": "YMK3540",
"Longitude": "41.00000000",
"Latitude": "2.00000000"
}]
地图活动:
package com.example.panos.fleet_management;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
import java.util.Map.Entry;
import static com.example.panos.fleet_management.R.id.map;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
new MapRequest(this).execute();
}
@Override
public void onMapReady (GoogleMap googleMap){
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(40,41);
LatLng location2 = new LatLng(11,41);
mMap.addMarker(new MarkerOptions().position(sydney).title(getIntent().getStringExtra("dat")));
mMap.addMarker(new MarkerOptions().position(location2).title("dsader"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
首先,为什么你有排球进口而你没有使用它们?这是一种非常简单的方法,可以异步从网络中获取数据,而不需要AsyncTask和您的Activity。
你也在创造意图而不做任何事情。如果您坚持以您的方式使用它,请使用Intent发送LocalBroadcastManager并在您的活动中为其注册接收器,其中地图是
最后我发现了一个非常简单的问题答案。我唯一需要做的就是使mMap变量公共静态,然后从onPostexecute访问它并将数据放在那里。