현재 나의 목표는 특정 좌표에 대한 정보를 반환하도록 Google Map API를 구문 분석하는 것입니다.Android Reverse Geocoding TextView에 텍스트가 표시되지 않습니다.
내가 겪고있는 주요 문제는 프로그램을 실행할 때 표시 할 항목이 없다는 것입니다. 아래에 주어진 코드는 프로그램을 멈추는 데 아무런 오류없이 실행되지만 빈 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;
}
}
를 사용하는 몇 가지 예제 코드는? 그냥 궁금해서. – pjco
나는 그것에 대한 대답을 갖고 싶다. 이것은 안드로이드 프로그래밍과 관련된 첫 번째 학기이며 교수님이 저에게 주신 도움의 도움을받지 못했습니다. 더 나은 방향으로 바꾸는 방법에 대한 긍정적 인 의견을 보내 주시면 감사하겠습니다. –
Google 위치 API를 제안합니다. 다른 것들 중에서도 JSON 또는 HTTP를 다룰 필요가 없습니다. http://developer.android.com/google/play-services/location.html ... 게시 할 예정입니다. 대답은 – pjco