Gå til innhold

Android JSON problem


Anbefalte innlegg

Holder på å lage en Android app som skal hente noe data fra Google map. Dataen mottaes som JSON.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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.JSONArray;
import android.util.Log;

public class GoogleMapRequests
{
private static final String urlLocation = "http://maps.googleapis.com/maps/api/geocode/json?address=";
private static final String urlDistance = "http://maps.googleapis.com/maps/api/directions/json?origin=";
private static String result = null;
private static String line = null;
private static HttpGet httpGet = null;
private static DefaultHttpClient client = null;	
private static HttpResponse response = null;	
private static BufferedReader br = null;
private static StringBuilder sb = null;
private static InputStream is = null;

private static JSONArray sendRequest(String googleMapUrl)
{
	client = new DefaultHttpClient();		
	try
	{
		httpGet = new HttpGet(googleMapUrl);	
	}
	catch(Exception e)
	{
		Log.e("log_tag", "httpGet Error: "+e.toString());
	}

	try
	{
		response = client.execute(httpGet);
	}
	catch(ClientProtocolException cpeException)
	{
		cpeException.printStackTrace();
	}
	catch(IOException ioException)
	{
		ioException.printStackTrace();
	}

	try
	{
		is = response.getEntity().getContent();
		br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
		sb = new StringBuilder();			

		while((line = br.readLine()) != null)
			sb.append(line+"\n");

		is.close();
		result = sb.toString();
	}
	catch(Exception e)
	{
		Log.e("log_tag", "Error converting to string: "+e.toString());
	}

	JSONArray jArray = null;
	try
	{
		jArray = new JSONArray(result);		
	}
	catch(Exception e)
	{
		Log.e("log_tag", "Error parsing to JSON: "+e.toString());
		jArray = null;
	}
	return jArray;
}

public static JSONArray getLocation(String location)
{
	return sendRequest(urlLocation+location+"&sensor=false");
}
}

 

Dataen som mottas når String location=norge ser slik ut:

 

{
 "status": "OK",
 "results": [ {
   "types": [ "country", "political" ],
   "formatted_address": "Norway",
   "address_components": [ {
     "long_name": "Norway",
     "short_name": "NO",
     "types": [ "country", "political" ]
   } ],
   "geometry": {
     "location": {
       "lat": 60.4720240,
       "lng": 8.4689460
     },
     "location_type": "APPROXIMATE",
     "viewport": {
       "southwest": {
         "lat": 47.1204866,
         "lng": -24.3142579
       },
       "northeast": {
         "lat": 69.9406045,
         "lng": 41.2521499
       }
     },
     "bounds": {
       "southwest": {
         "lat": 57.8097000,
         "lng": 4.0649000
       },
       "northeast": {
         "lat": 71.3078000,
         "lng": 31.3550000
       }
     }
   }
 } ]
}

 

Dataen som mottaes fra google ser jo rett ut, og har også validert den uten noen problem i diverse JSON validatorer på nettet.

 

Problemet oppstår når stringen med data skal legges inn i en JSONArray.

 

JSONArray jArray = null;
try
{
    jArray = new JSONArray(result);		
}

 

Av en eller annen grunn godtar den ikke stringen og jeg får opp følgende error i Log:

 

org.json.JSONException: Value {"results":[{"formatted_address":"Norway"...org.json.JSONObject cannot be converted to JSONArray. Noen som vet hvor problemet er?

Endret av Frezz
Lenke til kommentar
Videoannonse
Annonse

Har funnet ut av problemet. Hadde misforstått hvordan JSONArray virker. For dere som måtte være interessert, så måtte jeg endre koden til:

 

JSONArray jArray = null;		
try
{
JSONObject jObj = new JSONObject(result);
String jsonStatus = jObj.getString("status");
jArray = jObj.getJSONArray("results");
if(jsonStatus.compareTo("OK") != 0)
	jArray = null;
}

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...