2017-07-12 119 views
0

是在Android Studio中新,我試圖讓使用排球,什麼都好我的簡單的應用程序得到URL JSON數據,但我在URL有問題,時間格式時間戳我想讓他顯示在本地時間 。從數據JSON URL轉換爲Timestamp

我的代碼

package imo.meteoiraq; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.TextView; 

    import com.android.volley.Request; 
    import com.android.volley.RequestQueue; 
    import com.android.volley.Response; 
    import com.android.volley.VolleyError; 
    import com.android.volley.toolbox.JsonObjectRequest; 
    import com.android.volley.toolbox.Volley; 

    import org.json.JSONException; 
    import org.json.JSONObject; 

    import java.security.Timestamp; 

    public class MainActivity extends AppCompatActivity { 
    RequestQueue rq; 
    TextView timeDesc,tempDesc,windspeedDesc,windguestDesc,humdityDesc; 
     int ages; 
     int temp; 
     int windspeed; 
     int windguest; 
     int humdity; 
     String timeupdate; 

     String url="/stationlookup?station=I1410&units=metric&v=2.0&format=json"; 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      rq= Volley.newRequestQueue(this); 

      timeDesc= (TextView) findViewById(R.id.timeupdateDesc); 
      tempDesc= (TextView) findViewById(R.id.tempid); 
      windspeedDesc= (TextView) findViewById(R.id.windid); 
      windguestDesc= (TextView) findViewById(R.id.windgustid); 
      humdityDesc= (TextView) findViewById(R.id.humdid); 

      sendjsonrequest(); 
     } 
    public void sendjsonrequest(){ 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 
        JSONObject stationsJO = response.getJSONObject("stations"); 
        JSONObject I1410JO = stationsJO.getJSONObject("I1410"); 
        temp = I1410JO.getInt("temperature"); 
        windspeed = I1410JO.getInt("wind_speed"); 
        windguest = I1410JO.getInt("wind_gust_speed"); 
        humdity = I1410JO.getInt("humidity"); 
        timeupdate = I1410JO.getString(Timestamp,"updated"); 

        timeDesc.setText(timeupdate); 
        tempDesc.setText(Integer.toString(temp)); 
        windspeedDesc.setText(Integer.toString(windspeed)); 
        windguestDesc.setText(Integer.toString(windguest)); 
        humdityDesc.setText(Integer.toString(humdity)); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }); 
     rq.add(jsonObjectRequest); 
    } 


    } 

JSON

{ 
    "stations": { 
    "I1410": { 
     "updated": 1499842858, 
     "ageh": 0, 
     "agem": 1, 
     "ages": 6, 
     "type": "PWS", 
     "wind_dir_degrees": 270, 
     "wind_speed": 7.2, 
     "wind_gust_speed": 9.7, 
     "humidity": 12, 
     "temperature": 38.9, 
     "precip_rate": null, 
     "precip_today": 0, 
     "pressure": 1000.56, 
     "dewpoint": null, 
     "windchill": null 
    } 
    } 
} 
+0

你想顯示日期,而不是時間戳? –

+0

是的時間和數據 – aligassan

回答

3

對於時間戳轉換爲日期格式

結帳進口

//Imports are as below 
 
import java.text.DateFormat; 
 
import java.text.SimpleDateFormat; 
 
import java.util.Date; 
 

 
//convert timestamp (seconds) to milliseconds 
 
    String timeupdate = "1499842858"; 
 
    long timestamp = Long.parseLong(timeupdate) * 1000L; 
 
    Toast.makeText(getApplicationContext(),"Date:"+getDate(timestamp),Toast.LENGTH_SHORT).show(); 
 
    
 
    timeDesc.setText(getDate(timestamp)); 
 
     
 
     
 
    private String getDate(long timeStamp){ 
 
     try{ 
 
      DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss"); 
 
      Date netDate = (new Date(timeStamp)); 
 
      return sdf.format(netDate); 
 
     } 
 
     catch(Exception ex){ 
 
      return "xx"; 
 
     } 
 
    }

我檢查代碼和它的工作的罰款

+0

謝謝你的回答fatel錯誤<應用程序停止工作,我想他的數據和時間不僅僅是數據 – aligassan

+0

結帳編輯代碼使用「MM/dd/yyyy hh:mm:ss」日期和時間 –

+0

爲什麼我有應用程序停止工作?何時調試?如果我刪除你的代碼工作正常! – aligassan

1

您可以使用此代碼:

long millisecond = I1410JO.getLong("updated")*1000L; 
    timeupdate = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString(); 
    timeDesc.setText(timeupdate); 
+0

感謝答案fatel錯誤<應用程序停止工作,我希望他的數據和時間而不僅僅是數據 – aligassan