【问题标题】:Android Studio JSON doesn't return properlyAndroid Studio JSON 无法正确返回
【发布时间】:2016-08-19 17:16:04
【问题描述】:

我想问一些关于我正在尝试创建的应用程序的问题,当我打开调试器时,这些错误在 logcat 中不断重复:

08-19 07:01:10.318 32526-32526/? E/android.os.Debug: failed to load memtrack module: -2
08-19 07:01:10.658 32537-32537/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-19 07:01:10.658 32537-32537/? E/android.os.Debug: failed to load memtrack module: -2
08-19 07:01:10.658 3816-3816/? E/PGA: PgaSocketInitClientPgaIpc: ioctl() failed: ret -1, errno 22

我不知道是什么导致了问题,昨晚它工作得很好,应用程序仍然存在错误,但这不是错误。请帮我解决我的错误: 我正在开发一个天气应用程序,它从 forecast.io 获取天气并将其显示在屏幕上,现在我的布局尚未制作,但在调试器中测试我发现了问题。 从 getData(jsonData) 返回的 Current 没有填充 mCurrent 变量,现在我认为它没有正确返回,因为 Current 变量在我查看 getData(jsonData) 方法时被填充。

主活动

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    @BindView(R.id.locateBTN)
    ImageView mLocateButton;
    @BindView(R.id.locationTV)
    TextView mLocationText;
    @BindView(R.id.tempTV)
    TextView mTempText;
    @BindView(R.id.timeTV)
    TextView mTimeText;
    @BindView(R.id.locationET)
    EditText mLocationEText;
    private Current mCurrent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mLocationEText = (EditText) findViewById(R.id.locationET);
        mTimeText = (TextView) findViewById(R.id.timeTV);

        String APIKey = "HIDDEN FOR OBVIOUS REASONS";
        double longtitude = 41.1421743;
        double latitude = 22.4851028;

        Uri.Builder builder = new Uri.Builder();
        builder.scheme("https")
                .authority("api.forecast.io")
                .appendPath("forecast")
                .appendPath(APIKey)
                .appendPath(longtitude + "," + latitude)
                .appendQueryParameter("units", "si");
        String URL = builder.build().toString();

        if(isNetworkAvailible()) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(URL)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                    if(response.isSuccessful()){
                        try {
                            mCurrent =  getData(jsonData);
                            updateVariables();
                        } catch (JSONException e){
                            Log.e(TAG, "Exception caught:", e);
                        }
                    }

                }
            });
        }else{
            Toast.makeText(this, "Can't get DATA", Toast.LENGTH_SHORT).show();
        }
    }


    public boolean isNetworkAvailible() {
        ConnectivityManager manager = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = manager.getActiveNetworkInfo();
        boolean isAvailible = false;
        if (netInfo != null) {
            isAvailible = true;
        }
        return isAvailible;
    }
    private Current getData(String jsonData) throws JSONException{
            JSONObject forecast = new JSONObject(jsonData);
            String timezone = forecast.getString("timezone");
            Log.i(TAG, "JSON" + timezone);
            JSONObject currently = forecast.getJSONObject("currently");

            Current Current = new Current();
            Current.setTime(currently.getLong("time"));
            Current.setTemp(currently.getDouble("temperature"));
            Current.setIcon(currently.getString("icon"));
            Current.setPrecip(currently.getDouble("precipProbability"));
            Current.setSummary(currently.getString("summary"));
            Current.setTimeZone(timezone);

            Log.d(TAG, Current.getFormattedTime());

            return Current;
    }
    private void updateVariables(){
        mTempText.setText((int) mCurrent.getTemp());
    } }

当前

public class Current {
    private long mTime;
    private String mSummary;
    private String mIcon;
    private double mTemp;
    private double mPrecip;
    private String mTimeZone;


    public String getTimeZone() {
        return mTimeZone;
    }

    public void setTimeZone(String timeZone) {
        mTimeZone = timeZone;
    }

    public long getTime() {
        return mTime;
    }

    public void setTime(long time) {
        mTime = time;
    }

    public String getFormattedTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
        formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
        Date datetime = new Date(getTime() * 1000);
        String timeString = formatter.format(datetime);

        return timeString;
    }

    public String getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        mSummary = summary;
    }

    public String getIcon() {
        return mIcon;
    }

    public void setIcon(String icon) {
        mIcon = icon;
    }

    public double getTemp() {
        return mTemp;
    }

    public void setTemp(double temp) {
        mTemp = temp;
    }

    public double getPrecip() {
        return mPrecip;
    }

    public void setPrecip(double precip) {
        mPrecip = precip;
    } }

【问题讨论】:

  • 您是否已将互联网权限添加到清单文件中?
  • 是的,正如我所说,它填充了 getData() 方法,但它不返回它。而现在,我最大的问题是模拟器,因为我没有 Andorid 设备,我使用的是 Lumia 1020。

标签: java android json android-studio


【解决方案1】:

我修复了它,这里发布的代码正在运行,问题是我在发布之前尝试修复它,但由于上述原因无法测试。 我通过更改模拟器修复了 Android 错误。 我现在使用 Bluestacks 模拟器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多