2016-08-19 86 views
0

我要問一些有關我的應用程序,我想創建當我打開調試器,這些誤差保持在logcat中永遠重複:Android Studio中JSON不返回正確

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

您是否已將互聯網權限添加到清單文件中? – Dnyanesh

+0

是的,我說過它會填充getData()方法,但它不會返回它。 而現在,我更大的問題是模擬器,因爲我沒有Andorid設備,所以我使用了Lumia 1020。 – lakimens

回答

0

我固定它,在這裏發佈的代碼工作的事情是我試圖修復它之前,我張貼在這裏,但由於無法測試上面列出的原因。 我通過更改模擬器修復了Android錯誤。 我現在使用Bluestacks模擬器。