Android反向地理编码文本视图中不显示文本
我目前的目标是解析GoogleMapAPI,以返回有关特定坐标的信息 我遇到的主要问题是,当我运行程序时,无法显示任何内容。下面给出的代码在停止程序时不会出现任何错误,但只会生成一个空白的文本视图。我不太熟悉JSON解析(这是我使用过的第一个程序),我想知道缺少了什么阻止返回的信息显示在TextView上 在通过JSON进行解析时,是否有一种特定的方法来获取要显示的文本?我感谢所有的帮助 My MainActivity.java类:Android反向地理编码文本视图中不显示文本,android,json,parsing,textview,geocoding,Android,Json,Parsing,Textview,Geocoding,我目前的目标是解析GoogleMapAPI,以返回有关特定坐标的信息 我遇到的主要问题是,当我运行程序时,无法显示任何内容。下面给出的代码在停止程序时不会出现任何错误,但只会生成一个空白的文本视图。我不太熟悉JSON解析(这是我使用过的第一个程序),我想知道缺少了什么阻止返回的信息显示在TextView上 在通过JSON进行解析时,是否有一种特定的方法来获取要显示的文本?我感谢所有的帮助 My MainActivity.java类: import org.json.JSONArray; impo
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.widget.TextView;
public class MainActivity extends Activity {
// URL to make the request to
private static String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.1031,-75.1522&sensor=true";
// JSON Node names
private static final String TAG_LOCATION = "results";
private static final String TAG_CITY = "long_name";
// The JSON Array
JSONArray location = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Location
location = json.getJSONArray(TAG_LOCATION);
// looping through each part of location
for(int i = 0; i < location.length(); i++){
JSONObject c = location.getJSONObject(i);
// Storing each JSON item in a variable
String city = c.getString(TAG_CITY);
// For whichever one works
System.out.println(city);
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText("City: " + city);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
package com.example.finalproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
long\u name
键在address\u components
JSONArray中的JSONObject中,而不是results
JSONArray中的JSONObject,因此您应该首先从c JSONObject获取JSONArray,如下所示:
for(int i = 0; i < location.length(); i++){
JSONObject c = location.getJSONObject(i);
// get address_components JSONArray from c
JSONArray add_array=c.getJSONArray("address_components");
for(int j = 0; j < add_array.length(); j++){
JSONObject obj_add = add_array.getJSONObject(i);
// Storing each JSON item in a variable
String city = c.getString(TAG_CITY);
//your code here....
}
}
for(int i=0;i
并且还用于从weservice获取数据,而不是在主UI线程上执行网络操作。
long\u name
键位于JSONObject中,它位于address\u组件中,而不是位于results
JSONArray中的JSONObject,因此您应该首先从c JSONObject中获取JSONArray,如下所示:
for(int i = 0; i < location.length(); i++){
JSONObject c = location.getJSONObject(i);
// get address_components JSONArray from c
JSONArray add_array=c.getJSONArray("address_components");
for(int j = 0; j < add_array.length(); j++){
JSONObject obj_add = add_array.getJSONObject(i);
// Storing each JSON item in a variable
String city = c.getString(TAG_CITY);
//your code here....
}
}
for(int i=0;i
并且还用于从weservice获取数据,而不是在主UI线程上执行网络操作。下面是一些使用Android/Google location API的示例代码
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
private void reverseGeocode()
{
AsyncTask< LatLng, Integer, List< Address > > task
= new AsyncTask< LatLng, Integer, List< Address > >()
{
@Override
protected List< Address > doInBackground( LatLng... params )
{
LatLng latLng = params[0];
Geocoder geoCoder = new Geocoder(getActivity().getApplicationContext() );
List< Address > matches = null;
try
{
matches = geoCoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
}
catch ( IOException e )
{
e.printStackTrace();
}
return matches;
}
@Override
protected void onPostExecute( List< Address > result )
{
if ( result != null && result.size() > 0 )
{
if ( D ) Log.v( TAG, "onPostExecute result size=" + result.size() );
Address bestMatch = (result.isEmpty() ? null : result.get(0));
//showGeocodedAddress( bestMatch );
}
}
};
task.execute( latLng );
}
导入android.location.Address;
导入android.location.Criteria;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationManager;
私有void reverseGeocode()
{
异步任务>任务
=新的异步任务>()
{
@凌驾
受保护列表doInBackground(LatLng…params)
{
LatLng LatLng=参数[0];
Geocoder Geocoder=新的地理编码器(getActivity().getApplicationContext());
列表<地址>匹配项=空;
尝试
{
matches=geoCoder.getFromLocation(latLng.latitude,latLng.longitude,1);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
返回比赛;
}
@凌驾
受保护的void onPostExecute(列表<地址>结果)
{
if(result!=null&&result.size()>0)
{
如果(D)Log.v(标记“onPostExecute结果大小=“+result.size()”);
地址最佳匹配=(result.isEmpty()?null:result.get(0));
//showGeocodedAddress(最佳匹配);
}
}
};
任务执行(latLng);
}
以下是一些使用Android/Google location API的示例代码
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
private void reverseGeocode()
{
AsyncTask< LatLng, Integer, List< Address > > task
= new AsyncTask< LatLng, Integer, List< Address > >()
{
@Override
protected List< Address > doInBackground( LatLng... params )
{
LatLng latLng = params[0];
Geocoder geoCoder = new Geocoder(getActivity().getApplicationContext() );
List< Address > matches = null;
try
{
matches = geoCoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
}
catch ( IOException e )
{
e.printStackTrace();
}
return matches;
}
@Override
protected void onPostExecute( List< Address > result )
{
if ( result != null && result.size() > 0 )
{
if ( D ) Log.v( TAG, "onPostExecute result size=" + result.size() );
Address bestMatch = (result.isEmpty() ? null : result.get(0));
//showGeocodedAddress( bestMatch );
}
}
};
task.execute( latLng );
}
导入android.location.Address;
导入android.location.Criteria;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationManager;
私有void reverseGeocode()
{
异步任务>任务
=新的异步任务>()
{
@凌驾
受保护列表doInBackground(LatLng…params)
{
LatLng LatLng=参数[0];
Geocoder Geocoder=新的地理编码器(getActivity().getApplicationContext());
列表<地址>匹配项=空;
尝试
{
matches=geoCoder.getFromLocation(latLng.latitude,latLng.longitude,1);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
返回比赛;
}
@凌驾
受保护的void onPostExecute(列表<地址>结果)
{
if(result!=null&&result.size()>0)
{
如果(D)Log.v(标记“onPostExecute结果大小=“+result.size()”);
地址最佳匹配=(result.isEmpty()?null:result.get(0));
//showGeocodedAddress(最佳匹配);
}
}
};
任务执行(latLng);
}
为什么要使用web API而不是Android API的常规谷歌地图?只是好奇。我希望我能回答这个问题。这是我学习Android编程的第一个学期,教授给了我很多帮助。如有任何关于如何改进的积极反馈,我们将不胜感激!我建议使用Google Location API,您不必处理JSON或HTTP等问题:……下面会给出答案为什么您使用的是web API而不是常规的Google Maps for Android API?只是好奇。我希望我能回答这个问题。这是我第一个学期学习Android编程,当时我正从h