Android Studio JSON无法正常返回

时间:2016-08-19 17:16:04

标签: java android json android-studio

我打算询问一下我正在尝试创建的应用程序,当我打开调试器时,这些错误会在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变量,现在我认为它没有正确返回,因为当我查看getData(jsonData)方法时,Current变量被填充。

MainActivity

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;
    } }

1 个答案:

答案 0 :(得分:0)

我修好了,这里发布的代码是有效的,事情是我在发布之前尝试修复它,但由于上面列出的原因无法测试。 我通过更改模拟器修复了Android错误。 我现在使用Bluestacks模拟器。