Here is my code, what I am trying to do is get the 4 fields shown from the data file, and then I will want to do an if statement to only get certain ones that are within a distance. What am I doing wrong and why is it saying there is a problem?
package com.dredaydesigns.radiostationfinder;
import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {
// String HTTPRadioURL = "https://transition.fcc.gov/fcc-bin/fmq?state=&call=&city=&arn=&serv=FC&vac=3&freq=0.0&fre2=107.9&facid=&asrn=&class=&dkt=&list=0&dist=100&dlat2="
// + latitude + "&mlat2=&slat2=&NS=N&dlon2="
// + longitude +"&mlon2=&slon2=&EW=W&size=9";
public static final String TAG = MainActivity.class.getSimpleName();
private RadioData mRadioData;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
// @Bind(R.id.longitudeLabel)
TextView mLongitudeLabel;
//@Bind(R.id.latitudeLabel)
TextView mLatitudeLabel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_content);
ButterKnife.bind(this);
double latitude = 32;
double longitude = -96;
double latitudeStations;
double longitudeStations;
RadioData mRadioData = new RadioData();
//String radioFinderURL = "http://data.fcc.gov/lpfmapi/rest/v1/lat/" + latitude + "/long/" + longitude + "?format=json&secondchannel=true";
//String HTTPRadioURL = "https://transition.fcc.gov/fcc-bin/fmq?state=&call=&city=&arn=&serv=FC&vac=3&freq=0.0&fre2=107.9&facid=&asrn=&class=&dkt=&list=0&dist=100&dlat2="
// + latitude + "&mlat2=&slat2=&NS=N&dlon2="
// + longitude +"&mlon2=&slon2=&EW=W&size=9";
OkHttpClient client = new OkHttpClient();
String radioFinderURL = "http://dredaycreative.com/json/radioData.json";
Request request = new Request.Builder()
.url(radioFinderURL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mRadioData = getCurrentRadioData(jsonData);
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught: ", e);
} catch (JSONException e) {
Log.e(TAG, "Exception Caught:", e);
}
}
});
}
private void getCurrentRadioData(String jsonData) throws JSONException{
//private int mRadius = distanceBetween( double latitude, double longitude,
// double latitudeStations, double longitudeStations, float[] results)
//
// if {
// 500 >= mRadius;
// RadioData radioData = new RadioData();
List<String> radioData = new ArrayList<String>();
Object obj=JSONValue.parse(jsonData);
JSONArray data = (JSONArray)obj;
for (int i=0; i<data.length(); i++) {
JSONObject jasonObj = data.getJSONObject(i);
String callsign = jasonObj.getString("FIELD1");
radioData.add(callsign);
// Like this you can fetch whole data
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeLabel.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeLabel.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
and this is the logcat I am now receiving:
08-18 17:18:04.132 30705-30747/? E/AndroidRuntime﹕ FATAL EXCEPTION: OkHttp Dispatcher
Process: com.dredaydesigns.radiostationfinder, PID: 30705
java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray
at com.dredaydesigns.radiostationfinder.MainActivity.getCurrentRadioData(MainActivity.java:105)
at com.dredaydesigns.radiostationfinder.MainActivity.access$000(MainActivity.java:29)
at com.dredaydesigns.radiostationfinder.MainActivity$1.onResponse(MainActivity.java:79)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:170)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)