![](/img/trans.png)
[英]Media Controller properly isn't working for VideoView in Android
[英]Android getInputStream Isn't Working Properly
我正在嘗試創建一種從網站獲取位置高度的方法: https : //maps.googleapis.com/maps/api/elevation/json? locations = 0,0(其中0,0可以是替換為任何坐標)。
但是,從URL連接獲取InputStream
時遇到問題。
private double altitude(double lat, double longi) {
String elevation = "";
try {
URL alt = new URL("https://maps.googleapis.com/maps/api/elevation/json?locations="+lat+","+longi);
HttpURLConnection altFind = (HttpURLConnection) alt.openConnection();
altFind.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(altFind.getInputStream()));
String inputLine;
OUT:
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains("elevation")) {
for (int i = 23; ; i++) {
if (inputLine.charAt(i) != ',') {
elevation = elevation + "" + inputLine.charAt(i);
} else {
break OUT;
}
}
}
}
return Double.parseDouble(elevation);
} catch (MalformedURLException e) {}
catch (IOException e) {}
return 0;
}
那就是方法,我從這里調用它:
private TextView t;
private LocationManager locationManager;
private LocationListener listener;
double elev;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Intent intent = getIntent();
String longlat = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
longlat = longlat.replaceAll(" ", "");
String [] longilati = longlat.split(",");
final double [] coord = new double[2];
coord[0] = Double.parseDouble(longilati[0]);
coord[1] = Double.parseDouble(longilati[1]);
t = (TextView) findViewById(R.id.textView);
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
elev = altitude(coord[0], coord[1]);
} else {
Toast.makeText(this, "No connection available", Toast.LENGTH_LONG).show();
}
我曾嘗試遵循類似的問題,但無濟於事。
您應該在后台線程上執行網絡連接。
您可以使用Asynctask
類並在doInBackground()
內部移動altitude
方法。
您可以在此處閱讀更多信息: https : //developer.android.com/reference/android/os/AsyncTask.html
編輯:
我發現了一個名為JSONParser
類,我認為這是您所需要的,請查看以下內容: https : JSONParser
復制並將其粘貼到您的項目中。
您可以通過從JSONParser
類調用makeHttpRequest
方法並傳遞所有需要的參數來修改altitude
方法。 如果操作正確,則該方法將從URL返回JSON對象。 例如:
private static final String URL = "https://maps.googleapis.com/maps/api/elevation/json";
private static final String METHOD = "GET"; // You can use POST either
private double altitude(double lat, double lng) {
double elevation = 0.0;
HashMap<String, String> params = new HashMap<>();
params.put("locations", lat + "," + lng);
// Here you can put another params if needed
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.makeHttpRequest(URL, METHOD, params);
if(jsonObject != null) {
Log.d(TAG, jsonObject.toString()); // Check if data has arrived safely?
try {
JSONArray results = jsonObject.getJSONArray("results");
JSONObject result = results.getJSONObject(0);
elevation = result.getDouble("elevation");
} catch (JSONException e) {
e.printStackTrace();
}
return elevation;
} else {
return -1;
}
}
在doInBackground
內部調用altitude
並查看結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.