我目前的目标是解析 Google Map API 以返回有关特定坐标的信息。
我遇到的主要问题是运行程序时无法显示任何内容。下面给出的代码将运行而没有任何错误停止程序,但只会产生一个空白的 TextView。我对 JSON 解析不是很熟悉(这是我用它工作的第一个程序),我想知道缺少什么阻止返回的信息显示在 TextView 上。
在通过 JSON 解析时,是否有一种特定的方法可以让文本出现?感谢所有帮助。
我的 MainActivity.java 类:
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();
}
}
}
我的 JSONParser.java 类:
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_name
键在 JSONObject 中,它在 address_components
JSONArray 中,而不是在 results
JSONArray 中的 JSONObject,因此您应该首先获取 JSONArray来自 c JSONObject 作为:
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....
}
}
并使用 AsyncTask从 weservice 获取数据,而不是在主 UI 线程上进行网络操作。
关于Android 反向地理编码在 TextView 中不显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484966/