-2

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)
dreday
  • 95
  • 1
  • 9
  • 2
    `why is it saying there is a problem` what problem? – njzk2 Aug 18 '15 at 20:19
  • I said that already on another question. your data is not a json object, it is a json array. and the exception you are probably receiving states this very clearly. – njzk2 Aug 18 '15 at 20:20

1 Answers1

1
   List<String> radioData = new ArrayList<String>();

    Object obj=JSONValue.parse("**String you have**");
    JSONArray data = (JSONArray)obj
    for (int i=0; i<data.length(); i++) {
        JSONObject jasonObj = data.getJSONObject(i);

        String field1 = actor.getString("FIELD1");
        radioData.add(field1);

        // Like this you can fetch whole data



    }
  • thank you! where does the "actor" come from though? it is having trouble recognizing "actor" and jsonResponse? – dreday Aug 18 '15 at 21:10
  • 1
    sorry, its jasonObj. I changed from my code , so I missed spell it. – Ruchir Vani Aug 18 '15 at 21:12
  • thanks! but any idea why it cant recognize jsonResponse? – dreday Aug 18 '15 at 21:13
  • 1
    http://stackoverflow.com/questions/12289844/difference-between-jsonobject-and-jsonarray – Ruchir Vani Aug 18 '15 at 21:19
  • where do i get the jsonResponse from? it cannot resolve symbol? – dreday Aug 18 '15 at 21:29
  • have updated my code with terminal if you can please help! thanks! – dreday Aug 18 '15 at 21:37
  • nvm I found this and then made it a file in my project! https://code.google.com/p/json-simple/source/browse/trunk/src/org/json/simple/JSONValue.java?r=2 – dreday Aug 18 '15 at 21:53
  • I am now getting this in console see posted above! – dreday Aug 18 '15 at 22:19
  • JSONArray array = new JSONArray("String You have"); for(int i=0; i – Ruchir Vani Aug 19 '15 at 00:20
  • 1
    I just ran this code on my local machine, it is working fine, So try now – Ruchir Vani Aug 19 '15 at 00:22
  • I am and it is returning a bit of data but then I am getting this 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:28) at .... – dreday Aug 19 '15 at 00:27
  • I saw this post but if I do as he says and change it to .simple import statement it messes up and can't find the length function http://stackoverflow.com/questions/20652284/java-lang-classcastexception-org-json-simple-jsonarray-cannot-be-cast-to-org-js "Check your import statements at the top of your source file. You probably have a line saying: import org.json.JSONArray; It should instead be: import org.json.simple.JSONArray;" – dreday Aug 19 '15 at 00:29