Java 根据stackoverflow post(嵌入式)使用修复程序后,为什么现在出现无法解决方法长度错误?
我在一个项目上工作了一段时间,遇到了一个奇怪的不能抛出异常。我在这篇文章里找到了阿斯纳 但是,当我按照它的建议修改导入语句时,我现在出现了两个新的错误 它们是错误:(90,39)错误:找不到符号方法getJSONObject(int) 错误:(89,29)错误:找不到符号方法长度() 这是我的MainActivity代码Java 根据stackoverflow post(嵌入式)使用修复程序后,为什么现在出现无法解决方法长度错误?,java,android,Java,Android,我在一个项目上工作了一段时间,遇到了一个奇怪的不能抛出异常。我在这篇文章里找到了阿斯纳 但是,当我按照它的建议修改导入语句时,我现在出现了两个新的错误 它们是错误:(90,39)错误:找不到符号方法getJSONObject(int) 错误:(89,29)错误:找不到符号方法长度() 这是我的MainActivity代码 import android.R; import android.app.Activity; import android.location.Location; import
import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
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.simple.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {
public static final String TAG = MainActivity.class.getSimpleName();
private RadioData mRadioData;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_content);
ButterKnife.bind(this);
Location locationUserLatitude = mLastLocation;
Location locationUserLongitude = mLastLocation;
Location latitudeStation;
Location longitudeStation;
RadioData mRadioData = new RadioData();
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()) {
getCurrentRadioData();
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught: ", e);
} catch (JSONException e) {
Log.e(TAG, "Exception Caught:", e);
}
}
});
}
private List getCurrentRadioData() throws JSONException{
List radioData = new ArrayList<String>();
List localRadioData = new ArrayList();
Object obj=JSONValue.parse(String.valueOf(radioData));
JSONArray data;
data = (JSONArray)obj;
float[] results = new float[3];
double latitudeStation = 0;
double longitudeStation = 0;
for (int i=0; i<data.length(); i++) {
JSONObject jasonObj = data.getJSONObject(i);
double latitudeStations = jasonObj.getDouble("FIELD21");
radioData.add(latitudeStations);
double longitudeStations = jasonObj.getDouble("FIELD24");
radioData.add(longitudeStations);
Location.distanceBetween(mLastLocation.getLatitude(), mLastLocation.getLongitude(), latitudeStation, longitudeStation, results);
float distance = results[0];
distance = Math.round(distance);
if ((distance) > 350) {
JSONObject callsign = jasonObj.getJSONObject("FIELD1");
localRadioData.add(callsign);
JSONObject frequency = jasonObj.getJSONObject("FIELD2");
localRadioData.add(frequency);
} else {
radioData.add(getCurrentRadioData());
}
}
return localRadioData;
}
private void updateDisplay() {
}
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) {
}
}
活动主xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#ffffed">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
jsonParse
package com.dredaydesigns.radiostationfinder;
/*
* $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
* Created on 2006-4-15
*/
import org.json.simple.parser.JSONParser;
import java.io.Reader;
import java.io.StringReader;
/**
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class JSONValue {
/**
* parse into java object from input source.
* @param in
* @return instance of : JSONObject,JSONArray,String,Boolean,Long,Double or null
*/
public static Object parse(Reader in){
try{
JSONParser parser=new JSONParser();
return parser.parse(in);
}
catch(Exception e){
return null;
}
}
public static Object parse(String s){
StringReader in=new StringReader(s);
return parse(in);
}
}
package com.dredaydesigns.radiostationfinder;
/*
*$Id:JSONValue.java,v1.12006/04/1514:37:04平台扩展$
*创建于2006年4月15日
*/
导入org.json.simple.parser.JSONParser;
导入java.io.Reader;
导入java.io.StringReader;
/**
*@作者方以东
*/
公共类JSONValue{
/**
*从输入源解析为java对象。
*@param-in
*@return实例:JSONObject、JSONArray、String、Boolean、Long、Double或null
*/
公共静态对象解析(读取器在中){
试一试{
JSONParser=新的JSONParser();
返回parser.parse(in);
}
捕获(例外e){
返回null;
}
}
公共静态对象解析(字符串s){
StringReader in=新的StringReader;
返回解析(in);
}
}
您正在使用两个不同的JSON库,这可能会在代码上产生冲突
您正在使用库中的JSONArray
和JSONValue
,以及库中的JSONObject
和JSONException
试着用其中一个。我希望这能解决你的问题
编辑:
出现第一个错误(在应用修复之前),因为您使用了JSONValue
class from来解析数据,并使用JSONArray
from来存储数据。这会导致类强制转换异常
应用修复程序后,您已经使用JSONValue
和JSONArray
类from来解析和存储数组形式的数据,并使用JSONObject
from来存储JSONArray
上的对象。在本例中,org.json.simple.JSONArray
类没有getJSONObject(int)
和length()
方法,其中asorg.json.JSONArray
有它们。所以你得到了这两个例外
package com.dredaydesigns.radiostationfinder;
/*
* $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
* Created on 2006-4-15
*/
import org.json.simple.parser.JSONParser;
import java.io.Reader;
import java.io.StringReader;
/**
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class JSONValue {
/**
* parse into java object from input source.
* @param in
* @return instance of : JSONObject,JSONArray,String,Boolean,Long,Double or null
*/
public static Object parse(Reader in){
try{
JSONParser parser=new JSONParser();
return parser.parse(in);
}
catch(Exception e){
return null;
}
}
public static Object parse(String s){
StringReader in=new StringReader(s);
return parse(in);
}
}