i'm working on an app that uses JSON Responses which has both JSONObject and JSONArray. Both the information are required to place markers on map. But i'm facing problem in parsing both.
This is my Response when I called my URl
{
-source: {
LS: " ABCDEF",
name: "XYXA",
point: "77.583859,12.928751"
},
-stores: [
-{
ph: null,
distance: "0.3",
LS: " abcd",
id: 1209,
name: "xyz",
point: "77.583835,12.926359"
},
-{
ph: null,
distance: "0.3",
LS: " abcd",
id: 1209,
name: "xyz",
point: "77.583835,12.926359"
}
]
}
This is how I tried to handle JSON response
public class JSONResponseHandler implements ResponseHandler<List<LatlongRec>> {
#Override
public List<LatlongRec> handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
List<LatlongRec> result = new ArrayList<LatlongRec>();
String JSONResponse = new BasicResponseHandler()
.handleResponse(response);
try {
JSONObject object = (JSONObject)new JSONTokener(JSONResponse)
.nextValue();
JSONArray stores = object.getJSONArray("stores");
for (int i = 0; i < stores.length(); i++) {
JSONObject tmp = (JSONObject) stores.get(i);
result.add(new LatlongRec(tmp.getString("point"), tmp
.getString("name"), tmp.getString("LS"), tmp
.getString("ph")));
Log.v("points", "" + tmp.getString("point"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
}
In this LatlongRec is a class where I have created constructor and getters.
This is my Main Activity
public class SecondActivity extends Activity {
private static final double CAMERA_LNG = 77.583859;
private static final double CAMERA_LAT = 12.928751;
// The Map Object
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String city = getIntent().getStringExtra("city");
String area1 = getIntent().getStringExtra("area");
String area = area1.trim();
String URL = "http://xyzxyz.search.json?query="
+ area + "&city=" + city + "&app_id=test";
new HttpGetTask().execute(URL);
}
private class HttpGetTask extends AsyncTask<String, Void, List<LatlongRec>> {
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
#Override
protected List<LatlongRec> doInBackground(String... params) {
HttpGet request = new HttpGet(params[0]);
JSONResponseHandler responseHandler = new JSONResponseHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
#SuppressLint("NewApi")
protected void onPostExecute(List<LatlongRec> result) {
// Get Map Object
mMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
if (null != mMap) {
// Add a marker for every earthquake
for (final LatlongRec rec : result) {
Log.v("pointM", "" + rec.getPoint());
Log.v("NAMES", "" + rec.getName());
String point = rec.getPoint();
String[] latlng = point.split(",");
String lat = latlng[0];
String lng = latlng[1];
Double lat1 = Double.parseDouble(lat);
Double lng1 = Double.parseDouble(lng);
Log.v("LATLNGM", "lat" + lng + "& lng " + lat);
// Add a new marker for each point
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lng1, lat1))
.title(rec.getName())
.snippet(rec.getLS() + rec.getPh())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.main_marker)));
// Setting a custom info window adapter for the google map
CameraPosition cp = new CameraPosition(new LatLng(CAMERA_LAT,
CAMERA_LNG), 15, 40, 90);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}
if (null != mClient)
mClient.close();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
How to get points in source object to my main activity so that i can set CAMERA_LAT and CAMERA_LNG to Lat and Lng from that point.?
I'm new to JSON please help me to solve this.
Thanking you
String point[] = jsonObject.getJSONObject("source").getString("point").split(",");
double lat = Double.parseDouble(point[0]);
double lon = Double.parseDouble(point[1]);
Should get you on your way to getting the points out.
If you are looking on getting them to another activity, you should put them into a Bundle and pass them to the next activity. Here is a post explaining how to pass a bundle.
Try out a deserializer. I used the google GSON library to solve a similar problem I had. Take a peek at how I parsed the heroes array in the json object.
public class GameDeserializer implements JsonDeserializer<Game> {
public Game deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject().get("game").getAsJsonObject();
String id = obj.get("id").getAsString();
Integer turn = obj.get("turn").getAsInt();
Integer maxTurns = obj.get("maxTurns").getAsInt();
Hero[] heroes = new Hero[4];
for (int i = 0; i < 4; i++) {
heroes[i] = context.deserialize(obj.get("heroes").getAsJsonArray().get(i), Hero.class);
}
Board board = context.deserialize(obj.get("board"), Board.class);
Hero player = context.deserialize(json.getAsJsonObject().get("hero"), Hero.class);
String token = json.getAsJsonObject().get("token").getAsString();
String viewUrl = json.getAsJsonObject().get("viewUrl").getAsString();
String playUrl = json.getAsJsonObject().get("playUrl").getAsString();
boolean finished = obj.get("finished").getAsBoolean();
return new Game(id, turn, maxTurns, heroes, board, player, token, viewUrl, playUrl, finished);
}
}
}
I wouldn't have answered, but your question is very ambiguous and I'm not exactly sure if you need help with json, or with a String. In the latter case, disregard my answer.
Related
Application fetches data from OpenWeatherMap API and works correctly when I indicate a city, e.g. "London". There is an error when I replace "London" with a variable "place".. Underneath you can find the code.
Can anyone help me?
**MAIN ACTIVITY**
public class MainActivity extends AppCompatActivity {
//create the fields on the Activity
private TextView cityName;
private TextView temp;
private TextView description;
private TextView humidity;
private TextView pressure;
private TextView wind;
private TextView sunrise;
private TextView sunset;
private TextView updated;
private Exception error;
Weather weather = new Weather();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up all the resources for the Views here
cityName = (TextView)findViewById(R.id.cityText);
temp = (TextView)findViewById(R.id.tempText);
description = (TextView)findViewById(R.id.CloudText);
humidity = (TextView)findViewById(R.id.HumidText);
pressure = (TextView)findViewById(R.id.PressureText);
wind = (TextView)findViewById(R.id.WindText);
sunrise = (TextView)findViewById(R.id.RiseText);
sunset = (TextView)findViewById(R.id.SetText);
updated = (TextView)findViewById(R.id.UpdateText);
//instantiate a CityPreference object from the MainActivity that
carries the city with it..
//render the data based on the city you get through the parsing
CityPreference cityPreference = new CityPreference(MainActivity.this);
renderWeatherData(cityPreference.getCity());
}
public void renderWeatherData (String city){
// we want to do all of our tasks in the background, use AsyncTask for this
WeatherTask weatherTask = new WeatherTask();
weatherTask.execute(new String[]{city} + "&units=metric");
}
//last parameter is Weather, this is what we will be populating
private class WeatherTask extends AsyncTask<String, Void, Weather>{
//AsyncTask <PARAMETERS, TASK, RESULT>
#Override
protected Weather doInBackground(String... params) {
//background computation
//data variable holds all the data that we have got..
//instantiate the weather client and get the weather data..
//getWeatherData gets all necessary data from HTTPClient
//getWeather parses all the data from the JSONParser
try {
String data = ((new WeatherHTTPClient().getWeatherData(params[0])));
weather = JSONWeatherParser.getWeather(data);
//create a log to test if everything is working
Log.v("Data:",weather.currentCondition.getDescription());
return weather;
} catch (Exception e) {
error = e;
}
return null;
}
//here you will populate the data so you can show it to the user
#Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
try {
//gets time and decimal formats and applies it to the data underneath
DateFormat df = DateFormat.getTimeInstance();
String sunriseDate = df.format(new Date(Weather.place.getSunrise()));
String sunsetDate = df.format(new Date(Weather.place.getSunset()));
String updateDate = df.format(new Date(Weather.place.getLastupdate()));
DecimalFormat decimalFormat = new DecimalFormat("#.#");
String tempFormat = decimalFormat.format(weather.currentCondition.getTemperature());
//gets the value from the JSON and parses it in the view of the activity
cityName.setText(weather.place.getCity() + "," + weather.place.getCountry());
temp.setText("" + tempFormat + "C");
humidity.setText("Humidity" + weather.currentCondition.getHumidity() + "%");
pressure.setText("Pressure" + weather.currentCondition.getPressure() + "hPa");
wind.setText("Wind" + weather.wind.getSpeed() + "mps");
sunrise.setText("Sunrise" + sunriseDate);
sunset.setText("Sunset" + sunsetDate);
updated.setText("Updated" + updateDate);
description.setText("Condition" + weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescription() + ")");
} catch(Exception e) {
error = e;
}
}
}
//create an alert dialog to put in the city you want to parse the data for - A subclass of Dialog that can display one, two or three buttons.
private void showInputDialog () {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Change City");
final EditText cityInput = new EditText(MainActivity.this);
cityInput.setInputType(InputType.TYPE_CLASS_TEXT);
cityInput.setHint("put down city name");
builder.setView(cityInput);
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which){
CityPreference cityPreference = new CityPreference(MainActivity.this);
cityPreference.setCity(cityInput.getText().toString());
String newCity = cityPreference.getCity();
renderWeatherData(newCity);
}
});
builder.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//inflate the menu, this adds items to the action bar if it is present
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.change_cityId){
showInputDialog();
}
return super.onOptionsItemSelected(item);
}
}
**HTTP CLIENT**
public class WeatherHTTPClient {
public String getWeatherData (String place) throws IOException {
// setting things up.., create connection between application and the web, everything we get from the web comes as an inputstream
HttpURLConnection connection = null;
InputStream inputStream = null;
// api.openweathermap.org/data/2.5/weather?q=London
// errors may occur when you connect to the internet, build this in the model with try...catch
WHEN I REPLACE "place" by "London" in the next statement, the application works. THERE MUST BE AN ERROR WITH THIS PART, BUT I CANNOT FIND IT.. I NEED HELP..
try {
connection = (HttpURLConnection) (new URL(Utils.BASE_URL + place +"&APPID=f77c39703fb6be71e2c5a96e58edc077")).openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
//read the response, buffer "bucket" where you are going to put your data
StringBuffer stringBuffer = new StringBuffer();
//you get a stream of bits and data to your device - everything comes in as an inputstream
inputStream = connection.getInputStream();
//BufferedReader is the only thing that can read the stream of data - hold it and start reading
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
//line is set as the data read by the BufferedReader, Stringbuffer reads the Buffer and goes to the next one
while ((line = bufferedReader.readLine()) != null){
stringBuffer.append(line + "\r\n");
}
inputStream.close();
connection.disconnect();
return stringBuffer.toString();
} catch (IOException e) {e.printStackTrace();}
return null;
}
}
**WEATHER DATA MODEL JSON**
public class Weather {
public static Place place;
public String IconData;
public CurrentCondition currentCondition = new CurrentCondition();
public Temperature temperature = new Temperature();
public Wind wind = new Wind();
public Snow snow = new Snow();
public Clouds clouds = new Clouds();
}
**PLACE DATA MODEL, left out getters and setters**
**LEFT OUT THE FULL DATA MODEL**
public class Place {
private float lon ;
private float lat ;
private long sunset;
private long sunrise;
private String country;
private String city;
private long lastupdate;
**THE WEATHER PARSER**
public class JSONWeatherParser {
public static Weather getWeather(String data){
//we call the top JSON object weather
Weather weather = new Weather();
//create JSONObject that holds all of the data you get from the web
//place is the parameter you use to get all the data for
//the rest of the variables you have to "calculate" from your data model
//tagName needs to be EXACTLY as it is in the JSON
try {
JSONObject jsonObject = new JSONObject(data);
Place place = new Place();
//for each "header" in JSON you create a JSONObject or a JSONArray
JSONObject coorObj = Utils.getObject("coord", jsonObject);
//you set the latitude by getting the "lat" variable from the coorObj from the top JSON Object weather
place.setLat(Utils.getFloat("lat",coorObj));
place.setLon(Utils.getFloat("lon",coorObj));
//get the sys object
JSONObject sysObj = Utils.getObject("sys", jsonObject);
place.setCountry(Utils.getString("country", sysObj));
//dt Lastupdate is found directly under the jsonObject, hence ...
place.setLastupdate(Utils.getInt("dt", jsonObject));
place.setSunrise(Utils.getInt("sunrise",sysObj));
place.setSunset(Utils.getInt("sunset",sysObj));
place.setCity(Utils.getString("name",jsonObject));
weather.place = place;
//get the weather info, it is a JSONArray of the main jsonObject (the whole thing) - starts with square brackets
JSONArray jsonArray = jsonObject.getJSONArray("weather");
//get the underlying Jsonobject from the jsonarray
JSONObject jsonWeather = jsonArray.getJSONObject(0);
weather.currentCondition.setWeatherId(Utils.getInt("id",jsonWeather));
weather.currentCondition.setDescription(Utils.getString("description", jsonWeather));
weather.currentCondition.setCondition(Utils.getString("main",jsonWeather));
weather.currentCondition.setIcon(Utils.getString("icon",jsonWeather));
JSONObject mainObj = Utils.getObject("main", jsonObject);
weather.currentCondition.setHumidity(Utils.getInt("humidity",mainObj));
weather.currentCondition.setPressure(Utils.getInt("pressure",mainObj));
weather.currentCondition.setMin_temp(Utils.getFloat("temp_min",mainObj));
weather.currentCondition.setMax_temp(Utils.getFloat("temp_max", mainObj));
weather.currentCondition.setTemperature(Utils.getDouble("temp",mainObj));
JSONObject windObj = Utils.getObject("wind", jsonObject);
weather.wind.setSpeed(Utils.getFloat("speed", windObj));
weather.wind.setDeg(Utils.getFloat("deg",windObj));
JSONObject cloudObj = Utils.getObject("clouds", jsonObject);
weather.clouds.setPrecipitation(Utils.getInt("all", cloudObj));
return weather;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
If you look at the URL
https://api.openweathermap.org/data/2.5/weather?q=[Ljava.lang.String;#34e4306&units=metric&APPID=f77c39703fb6be71e2c5a96e58edc077
The value for the query field (q) is [Ljava.lang.String;#34e4306, which is invalid. Ensure that you are sending a proper string value in this place.
Can this line be modified from:
weatherTask.execute(new String[]{city} + "&units=metric");
to the code below:
weatherTask.execute(city + "&units=metric");
Hope this would help.
I have a JSON data in the following format,
[
{
"name": "France",
"date_time": "2015-05-17 19:59:00",
"dewpoint": "17",
"air_temp": "10.8"
},
{
"name": "England",
"date_time": "2015-05-17 19:58:48",
"dewpoint": "13",
"air_temp": "10.6"
},
{
"name": "Ireland",
"date_time": "2015-05-17 19:58:50",
"dewpoint": "15",
"air_temp": "11.1"
}
]
I have a Google map set up already for the Android app, so i have a pass the name value between two activity(GoogleMaps.java & WeatherInfo.java), now when i click a point in Google Map, it will pass the name to WeatherInfo.java, i want get the weather data for that name.
for example: i click France point in the map, The WeatherInfo.class will get the name is "France" and print out the "date_time, dewpoint, air_temp" for that point.
My question is how can i get the Json data parsed only for the point i clicked in the map? Can anyone look at the for loop in my WeatherInfo.java class?
WeatherInfo.java
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray("");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString(TAG_NAME);
String date_time = c.getString(TAG_DATE);
String temp = c.getString(TAG_TEMP);
String dewpoint = c.getString(TAG_DEWPOINT);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_NAME, name);
contact.put(TAG_DATE, date_time);
contact.put(TAG_TEMP, temp);
contact.put(TAG_DEWPOINT, dewpoint);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
JSONArray array = (JSONArray)new JSONTokener(jsonStr).nextValue();
for(int i = 0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
String name = jsonObject.getString("name");
String date = jsonObject.getString("date_time");
...
}
Or
JSONArray array = (JSONArray)new JSONTokener(jsonStr).nextValue();
for(int i = 0; i<array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
City city = new City();
city.name = jsonObject.getString("name");
...
}
You could use Jackson json parser as follows:-
you will need a value object for the point data.
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Point {
private final String name;
private final String dateTime;
private final int dewpoint;
private final double airTemp;
#JsonCreator
public Point(#JsonProperty("name") final String name, #JsonProperty("date_time") final String dateTime, #JsonProperty("dewpoint") final int dewpoint, #JsonProperty("air_temp") final double airTemp) {
this.name = name;
this.dateTime = dateTime;
this.dewpoint = dewpoint;
this.airTemp = airTemp;
}
public String getName() {
return name;
}
public String getDateTime() {
return dateTime;
}
public int getDewpoint() {
return dewpoint;
}
public double getAirTemp() {
return airTemp;
}
}
Then this Jackson Object Mapper
// 2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
Point[] points = mapper.readValue(new File("points.json"), Point[].class);
for (Point point : points) {
System.out.println("" + point.getName());
System.out.println("" + point.getDateTime());
System.out.println("" + point.getDewpoint());
System.out.println("" + point.getAirTemp());
}
I want to return a string array from Async class back to the activity that is calling this asynchronous class that is job is to do the reverse geocoding.
So, from my activity I call the constructor of the class like this:
Double[] lat_long = new Double[] { Double.parseDouble(map_lat), Double.parseDouble(map_long) };
ReverseGeocodingTask reverseGeocoding = new ReverseGeocodingTask(getActivity().getApplicationContext());
reverseGeocoding.execute(lat_long);
And this is the code of the class:
class ReverseGeocodingTask extends AsyncTask<Double, Void, List<String>> {
public static List<String> LIST = new ArrayList<String>();
Context mContext;
public ReverseGeocodingTask(Context context) {
super();
mContext = context;
}
#Override
protected List<String> doInBackground(Double... params) {
Geocoder gc= new Geocoder(mContext, Locale.getDefault());
List<Address> addrList = null;
double latitude = params[0].doubleValue();
double longitude = params[1].doubleValue();
Log.d("LATLONG", latitude + ":" + longitude);
try {
addrList = gc.getFromLocation(latitude, longitude, 1);
if (addrList.size() > 0) {
//format location info
Address address = addrList.get(0);
LIST.add(address.getLocality());
LIST.add(address.getSubAdminArea());
LIST.add(address.getCountryName());
Log.d("LIST", LIST.get(0));
}
else{
Log.d("addrList SIZE", "=0");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return LIST;
}
#Override
protected void onPostExecute(List<String> result) {
if (result != null) {
Log.d("ON POST", result.get(0));
}
}
}
This is the logcat:
02-28 19:20:04.323 12275-14109/guide_me_for_all.guide_me_for_all D/LATLONG﹕ 34.681377999999995:33.039339
02-28 19:20:05.434 12275-14109/guide_me_for_all.guide_me_for_all D/addrList SIZE﹕ =0
I get correctly the latitude and longitude point as you can see from the Log.d(), BUT getFromLocation.size() is always 0.
This may be a problem with your GeoCoder service. If you're backend service for the device is not present or has other problems, you will get this response.
use isPresent to check if an implementation is present.
Also, see this post here:
Geocoder.getFromLocation throws IOException on Android emulator
And the docs mention that you need a backend service:
http://developer.android.com/reference/android/location/Geocoder.html
I want to get photos and place_id required by Google Place Details API of my current location.
Search nearby does not return places in my exact location. (current lat/lng returned by android location service).
Radar search requires a keyword. Please suggest.
Per the Google Place Search documentation the three things you need to provide are the KEY, LOCATION and RADIUS. I cut out a bunch of unnecessary code, but here's how I did something similar.
1) Get your current location
private void initializeMapLocation() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Location lastLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
setUserLocation(lastLocation);
}
}
private void setUserLocation(Location location) {
LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(currentLatLng));
}
2) Build your search URL. You can add extra parameters like a keyword if you want by appending them on, but it doesn't sound like you want that in this particular case.
private void buildAndInitiateSearchTask(String searchType) {
Projection mProjection = mMap.getProjection();
LatLng mProjectionCenter = mProjection.getVisibleRegion().latLngBounds
.getCenter();
searchURL.append("https://maps.googleapis.com/maps/api/place/nearbysearch/");
searchURL.append("json?");
searchURL.append("location=" + mProjectionCenter.latitude + "," + mProjectionCenter.longitude);
searchURL.append("&radius=" + calculateProjectionRadiusInMeters(mProjection));
searchURL.append("&key=YOUR_KEY_HERE");
new PlaceSearchAPITask().execute(searchURL.toString());
}
private double calculateProjectionRadiusInMeters(Projection projection) {
LatLng farLeft = projection.getVisibleRegion().farLeft;
LatLng nearRight = projection.getVisibleRegion().nearRight;
Location farLeftLocation = new Location("Point A");
farLeftLocation.setLatitude(farLeft.latitude);
farLeftLocation.setLongitude(farLeft.longitude);
Location nearRightLocation = new Location("Point B");
nearRightLocation.setLatitude(nearRight.latitude);
nearRightLocation.setLongitude(nearRight.longitude);
return farLeftLocation.distanceTo(nearRightLocation) / 2 ;
}
3) Send your request and display your results as an AsyncTask
private class PlaceSearchAPITask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... placesURL) {
StringBuilder placesBuilder = new StringBuilder();
for (String placeSearchURL : placesURL) {
HttpClient placesClient = createHttpClient();
try {
HttpGet placesGet = new HttpGet(placeSearchURL);
HttpResponse placesResponse = placesClient
.execute(placesGet);
StatusLine placeSearchStatus = placesResponse
.getStatusLine();
if (placeSearchStatus.getStatusCode() == 200) {
HttpEntity placesEntity = placesResponse
.getEntity();
InputStream placesContent = placesEntity
.getContent();
InputStreamReader placesInput = new InputStreamReader(
placesContent);
BufferedReader placesReader = new BufferedReader(
placesInput);
String lineIn;
while ((lineIn = placesReader.readLine()) != null) {
placesBuilder.append(lineIn);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return placesBuilder.toString();
}
#Override
protected void onPostExecute(String result) {
try {
JSONObject resultObject = new JSONObject(result);
// This is my custom object to hold the pieces of the JSONResult that I want. You would need something else for your particular problem.
mapData = new MapDataSource(resultObject.optJSONArray("results"));
} catch (JSONException e) {
e.printStackTrace();
}
if (mapData != null) {
// TODO - This is where you would add your markers and whatnot.
}
}
}
I have such JSONparser class:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
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, "UTF-8"), 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;
}
}
and such activity:
public class BankExchangersListActivity extends ExpandableListActivity {
private static String url;
// JSON Node names
private static final String TAG_Exchangers = "bank_exchangers";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_address = "address";
private static final String TAG_location_name = "location_name";
private static final String TAG_latitude = "latitude";
private static final String TAG_longitude = "longitude";
private static final String TAG_exchanger_type_name = "exchanger_type_name";
private static final String TAG_exchanger_curr_value = "value";
private static final String TAG_currency_list_name = "currency_list_name";
private static final String TAG_direction_of_exchange_name = "direction_of_exchange_name";
JSONArray banks = null;
JSONArray exc_currencies = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
String bank;
bank = this.getIntent().getStringExtra("Bank_id");
url = "****/**_**_***_list/"+bank+".json";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bank_exchangers_list);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
ArrayList result = new ArrayList();
try {
// Getting Array of Contacts
banks = json.getJSONArray(TAG_Exchangers);
// looping through All Contacts
for(int i = 0; i < banks.length(); i++){
JSONObject c = banks.getJSONObject(i);
exc_currencies = c.getJSONArray("currency_values");
HashMap<String, String> map2 = new HashMap<String, String>();
ArrayList secList = new ArrayList();
for(int k = 0; k < exc_currencies.length(); k++){
JSONObject m = exc_currencies.getJSONObject(k);
String currency = m.getString(TAG_exchanger_curr_value);
String currency_list_name = m.getString(TAG_currency_list_name);
String direction_of_exchange_name = m.getString(TAG_direction_of_exchange_name);
Log.e("wazzzup", currency); //here is trouble: how to do new array with linking to parent?
HashMap child = new HashMap();
child.put(TAG_exchanger_curr_value, currency );
child.put(TAG_currency_list_name, currency_list_name );
child.put(TAG_direction_of_exchange_name, direction_of_exchange_name );
secList.add(child);
}
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
Log.e("name", name); //here is trouble: how to do new array with linking to parent?
String address = c.getString(TAG_address);
String location_name = c.getString(TAG_location_name);
String latitude = c.getString(TAG_latitude);
String longitude = c.getString(TAG_longitude);
String exchanger_type_name = c.getString(TAG_exchanger_type_name);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_address, address);
map.put(TAG_location_name, location_name);
map.put(TAG_latitude, latitude);
map.put(TAG_longitude, longitude);
map.put(TAG_exchanger_type_name, exchanger_type_name);
// adding HashList to ArrayList
contactList.add(map);
result.add(secList);
}
} catch (JSONException e) {
e.printStackTrace();
}
/*ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.bank_exchanger_list_element,
new String[] { TAG_NAME, TAG_location_name, TAG_address, TAG_exchanger_type_name, TAG_latitude, TAG_longitude }, new int[] {
R.id.bank_e_n, R.id.nas_punkt_e_n , R.id.adress_obm_e_n , R.id.tip_obm_e_n , R.id.shirota_e_n , R.id.dolgota_e_n });
setListAdapter(adapter);*/
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
contactList,
R.layout.bank_exchanger_list_element,
new String[] { TAG_NAME, TAG_location_name, TAG_address, TAG_exchanger_type_name, TAG_latitude, TAG_longitude },
new int[] {
R.id.bank_e_n, R.id.nas_punkt_e_n , R.id.adress_obm_e_n , R.id.tip_obm_e_n , R.id.shirota_e_n , R.id.dolgota_e_n },
result, //something goes here
R.layout.exchanger_currencies,
new String[] {TAG_exchanger_curr_value, TAG_currency_list_name, TAG_direction_of_exchange_name},
new int[] { R.id.currencyvalue_e_n, R.id.currency_list_name_e_n, R.id.direction_of_exchange_e_n}
);
setListAdapter( expListAdapter );
ExpandableListView elv = (ExpandableListView) getExpandableListView();
for(int i=0; i < expListAdapter.getGroupCount(); i++)
elv.expandGroup(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bank_exchangers_list, menu);
return true;
}
}
On android 2.3.3 it works properly. But on 4.x i see errors, after searching I understood that I need to use AsyncTask. Is it true?
But how do I use it here? If there are any useful examples, please let me know.
Just how to get it working on Android 4.x?
You should already use AsyncTask under Android 2.x. It greatly improves responsiveness. Here is a fragment from my MEGA API library (currently under development):
private class AsyncRequestConnection extends AsyncTask<Void, Void, String> {
private final Request request;
public AsyncRequestConnection(Request request) {
this.request = request;
}
#Override
protected String doInBackground(Void... params) {
try {
HttpPost p = createRequestHttpMessage(request);
String resp = new String(stripResponse(getRequestClient().execute(p)));
Log.v(TAG, resp);
return resp;
} catch (Exception e) {
Log.e(TAG, "Cannot complete API request", e);
cancel(false);
return null;
}
}
#Override
protected void onCancelled() {
request.backoff();
if (request.hasReachedMaxBackoff()) {
request.cancel(R.string.error_internal);
} else {
requestQueue.enqueue(request);
}
requestConnection = null;
nextRequest();
}
#Override
protected void onPostExecute(String result) {
if (result != null) {
request.attachResponse(result);
request.handleResponse();
requestConnection = null;
nextRequest();
} else {
request.cancel(R.string.error_internal);
}
}
}
doInBackgroud() is the only method that is run on a different thread. Here, all your costly operations should happen.
getRequestClient() returns a HttpClient (AndroidHttpClient.newInstance(AGENT_NAME) or reused object for multiple requests in a row).
Since you are running muliple threads here, make sure doInBackground() does not access any global data structure. In my example, get getRequestClient() is sure to be only called from this location and there is only one such AsyncTask at any time. Otherwise you need some kind of mutex. Also, the Request object is sure to used by this class exlusively. Event handling (call-back methods) is implemented in the Request object as well but for simpler tasks you could simply do everything you want to do in onCancel() and onPostExecute().
If you only want to download a JSON object and parse it, you probably won't even need a constructor and private member variables. Simply replace the first Void by String to pass the URL string to doInBackground() and replace String by JSONObject.