[android]此错误是什么意思?和为什么会不断出现吗?

标签: Java Android
发布时间: 2017/3/19 14:21:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这里是我 MainActivity 类的一个项目在哪里我试图调用已预测信息的 JSON 格式的链接︰

public class MainActivity extends ActionBarActivity {

    public static final String TAG = MainActivity.class.getSimpleName();
    private CurrentWeather mCurrentWeather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String apiKey = "bf092d4688e81db384b589d7a225e061";
        double apiLat = 37.8267;
        double apiLong = -122.423;
        String forecastURL = "https://api.forecast.io/forecast/" + apiKey + "/" + apiLat + "," + apiLong;
        if (isNetworkOnline()) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(forecastURL).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().toString();
                        Log.v(TAG, response.body().string());
                        if (response.isSuccessful()) {
                            mCurrentWeather = getCurrentDetails(JsonData);
                        } else {
                            alertUserAboutError();
                        }
                    } catch (IOException | JSONException e) {
                        Log.e(TAG, "Caught exception: ", e);
                    }
                }
            });
        } else {
            Toast.makeText(this, getString(R.string.network_not_available), Toast.LENGTH_LONG).show();
        }
        Log.d(TAG, "Main UI code is running.");
    }

    private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
        // getting the first JSON object in the json formatted link.
        JSONObject forecast = new JSONObject(jsonData);
        String timeZone = forecast.getString("timezone");

        // getting the second JSON object in the json format by looking for the
        // keyword "currently"
        JSONObject currently = forecast.getJSONObject("currently");
        CurrentWeather currentWeather = new CurrentWeather();
        currentWeather.setHumidity(currently.getDouble("humidity"));
        currentWeather.setIcon(currently.getString("icon"));
        currentWeather.setPrecipChance(currently.getDouble("precipProbability"));
        currentWeather.setSummary(currently.getString("summary"));
        currentWeather.setTemperature(currently.getDouble("temperature"));
        currentWeather.setTime(currently.getLong("time"));
        currentWeather.setTimeZone(timeZone);
        // Displaying the formatted time from seconds to the format we
        // specified.
        Toast.makeText(MainActivity.this, currentWeather.getFormattedTime(), Toast.LENGTH_LONG).show();
        return currentWeather;
    }

    private boolean isNetworkOnline() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        } else {
            return false;
        }
    }
}

然而,此错误不断涌现,我不能确定此错误说和它出现的原因。

07-21 01:37:10.644  20859-20908/com.example.android.stormy E/MainActivity﹕ Caught exception:
org.json.JSONException: Value com.squareup.okhttp.internal.http.RealResponseBody@423e87e0 of type java.lang.String cannot be converted to JSONObject
        at org.json.JSON.typeMismatch(JSON.java:111)
        at org.json.JSONObject.<init>(JSONObject.java:158)
        at org.json.JSONObject.<init>(JSONObject.java:171)
        at com.example.android.stormy.MainActivity.getCurrentDetails(MainActivity.java:73)
        at com.example.android.stormy.MainActivity.access$100(MainActivity.java:22)
        at com.example.android.stormy.MainActivity$1.onResponse(MainActivity.java:54)
        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:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)

解决方法 1:

java.lang.String cannot be converted to JSONObject
        at org.json.JSON.typeMismatch(JSON.java:111)

这可能会出现因为 web service 使用的不返回 jsonObject

它可能会发生时您正在发送到您的网站的关键 service 是不正确的或它可能发生如果键值是一个重复一些数据库中的主键或它可能是您的网站 service ,原因。

可能的解决方案︰

确保其关键是正确和匹配你的 web service 键, debug ,并找出什么是返回 json 字符串,要知道是否其重复和变化值,您试图发送,请确保您的 web service 后更改为返回一个有效的值 GET 和发送 params 通过 URL

它可能造成联合国通缉人物或被添加在您撰写时 String 如果是这样的; 女的出去

赞助商