0
我使用http://maps.google.com/maps/api/geocode從某些座標獲取並解決問題。從Google API Geocode獲取正確的重音 - Android
當我在APP JSON響應,有加重的任何字符出來一個「錯誤」 ---> O = A 3
我的APP是UTF-8
這裏編碼是獲取代碼的部分是一個從API
JSONArray results = jsonObject.getJSONArray("results");
JSONObject r = results.getJSONObject(0);
JSONArray addressComponentsArray = r.getJSONArray("address_components");
JSONObject addressComponents = addressComponentsArray.getJSONObject(0);
numero = addressComponents.getString("short_name");
Log.i("Número", numero);
JSONObject addressComponents1 = addressComponentsArray.getJSONObject(1);
rua = addressComponents1.getString("long_name");
Log.i("Rua", rua);
而這裏的信息是全班
class EncontrarEndereco extends AsyncTask<String, String, JSONObject> {
ProgressDialog pDialog = new ProgressDialog(GPSActivity.this);
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog.setMessage("Aguarde, enquanto buscamos seu endereço");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
double lat = -19.971864410192393;
double lng = -43.97544760674483;
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true&language=pt®ion=BR");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
protected JSONObject doInBackground (String... args){
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
protected void onPostExecute(final JSONObject jsonObject) {
// Dismiss a caixa de dialogo depois de buscar todos os items
String numero;
String rua;
String bairro;
String cidade;
String estado;
String pais;
String endereco_compelto;
pDialog.dismiss();
Log.i("JSON string =>", jsonObject.toString());
try {
String status = jsonObject.getString("status");
Log.i("status", status);
if(status.equalsIgnoreCase("OK")){
JSONArray results = jsonObject.getJSONArray("results");
JSONObject r = results.getJSONObject(0);
JSONArray addressComponentsArray = r.getJSONArray("address_components");
JSONObject addressComponents = addressComponentsArray.getJSONObject(0);
numero = addressComponents.getString("short_name");
Log.i("Número", numero);
JSONObject addressComponents1 = addressComponentsArray.getJSONObject(1);
rua = addressComponents1.getString("long_name");
Log.i("Rua", rua);
JSONObject addressComponents2 = addressComponentsArray.getJSONObject(2);
bairro = addressComponents2.getString("long_name");
Log.i("Bairro ", bairro);
JSONObject addressComponents3 = addressComponentsArray.getJSONObject(3);
cidade = addressComponents3.getString("long_name");
Log.i("Cidade ", cidade);
JSONObject addressComponents5 = addressComponentsArray.getJSONObject(5);
estado = addressComponents5.getString("short_name");
Log.i("Estado ", estado);
JSONObject addressComponents6 = addressComponentsArray.getJSONObject(6);
pais = addressComponents6.getString("long_name");
Log.i("Pais ", pais);
endereco_compelto = rua + ", " + numero + " - " + bairro + ", " + cidade + " - " + estado + ", " + pais;
endereco.setText(endereco_compelto);
}
}catch (JSONException e) {
Log.e("testing","Failed to load JSON");
e.printStackTrace();
}
}
}