私はこのURLを呼び出しています
http://maps.googleapis.com/maps/api/geocode/json?latlng=18.550455,73.920148&sensor=true
そして、応答を取得します(質問に関係ないため、途中から多くの応答を削除しました.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Towards Wadgao Sheri",
"short_name" : "Towards Wadgao Sheri",
"types" : [ "route" ]
},
{
"long_name" : "411014",
"short_name" : "411014",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Towards Wadgao Sheri, Sainikwadi, Wadgaon Sheri, Pune, Maharashtra 411014, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.55131630,
"lng" : 73.91882219999999
},
"southwest" : {
"lat" : 18.55024130,
"lng" : 73.91866569999999
}
},
"location" : {
"lat" : 18.55077720,
"lng" : 73.91878240
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.55212778029150,
"lng" : 73.92009293029150
},
"southwest" : {
"lat" : 18.54942981970850,
"lng" : 73.91739496970848
}
}
},
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Sainikwadi",
"short_name" : "Sainikwadi",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "411014",
"short_name" : "411014",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Sainikwadi, Wadgaon Sheri, Pune, Maharashtra 411014, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.5551140,
"lng" : 73.92143690
},
"southwest" : {
"lat" : 18.5465270,
"lng" : 73.91406809999999
}
},
"location" : {
"lat" : 18.55092520,
"lng" : 73.91687659999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.5551140,
"lng" : 73.92143690
},
"southwest" : {
"lat" : 18.5465270,
"lng" : 73.91406809999999
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.50447520,
"lng" : 97.3955550
},
"southwest" : {
"lat" : 6.747138899999999,
"lng" : 68.16238590
}
},
"location" : {
"lat" : 20.5936840,
"lng" : 78.962880
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.50447520,
"lng" : 97.3955550
},
"southwest" : {
"lat" : 6.747138899999999,
"lng" : 68.16279560
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
コード
JsonObject response = new JsonObject().getAsJsonObject(jsonResults.toString());
>>>NPE JsonArray results = response.getAsJsonArray("results");
json からの応答は jsonResults にあります。2行目でNPEを取得しています
私は応答がnullだと思います、なぜだろう、ここで何が間違っているのですか? 応答jsonからjsonオブジェクトを作成しました。ここで本当にばかげた何かが欠けていることを知っています。
編集 :
Exception in thread "Thread-0" java.lang.NullPointerException
at com.mcruiseon.ivr.God.getGeoAddress(God.java:141)
編集2:
json レスポンスを少し編集しました。ご注意ください。また、 formatted_addressを抽出しようとしています
編集3:
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;