如何重新填充JSON字符串以获取新数据

时间:2015-04-24 14:46:07

标签: android json

我试图每隔60秒将JSON字符串{"latitude":53.86897577,"longitude":10.66560449,"formatted":"24.04.2015 16:26:35","route":4}发送到服务器,但我总是得到相同的数据。如何管理它以获取JSON字符串中的新数据。我试着设置计时器但没有成功。我可以看到xml文件中的数据如何更改但是如何通过serliziation实现这一点?

MainActivity类:

    public class MainActivity extends ActionBarActivity {

        int route_number;
        double pLong;
        double pLat;
        String formatted;


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


            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationListener ll = new myLocationListener();
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
            enableGPS(lm);

          PostData sender = new PostData();
        }   

     class myLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {


        if (location != null) {
            pLong = location.getLongitude();
            pLat = location.getLatitude();
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss",
                    java.util.Locale.getDefault());
            formatted = sdf.format(location.getTime());

        }
                 String jSONString=  convertToJSON(pLong, pLat, formatted);

                 sender.timer(jSONString);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
}

    private String convertToJSON(double pLong, double pLat, String formatted) {
        //envelop the data in JSON format.                  
        Data d = new Data(pLat, pLong, formatted,route_number);
        Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new DataSerializer()).create();
         //return gson.toJson(d);
         String a = gson.toJson(d);
        // System.out.println(a);
         return a;
    }
    }

PostData类:

public class PostData {
    String jSONString;
    Handler handler = new Handler();

    public PostData() {
        super();


    }

    public String getjSONString() {
        return jSONString;
    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    public void timer(String jSONString) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean run = true;
                while (run) {
                   handler.postDelayed(new Runnable() {
                       @Override
                       public void run() {
                           new MyAsyncTask().execute(jSONString);
                       }
                   }, 30000);
                }
            }
        }).start();
    }

    class MyAsyncTask extends AsyncTask<String, Integer, Void> {

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub

                System.out.println("The output of : doInBackground " +jSONString);

            return null;

        }

    }

}

}

使用第四个密钥传递JSON字符串,我从MainActivity中的内部类BroadcastReceiver获取:

{
  "latitude":53.86897577,
  "longitude":10.66560449,
  "formatted":"24.04.2015 16:26:35",
  "route":4

}

1 个答案:

答案 0 :(得分:1)

您似乎只需要在timer()中致电onLocationChanged()即可。只需更改timer()即可获取jSONString,而不是在构造函数中设置它。

另一个主要问题是,您应该引用传递到varargs的{​​{1}}。

请注意,我在此处提议的内容会删除您的背景AsyncTask,并且只会在位置发生变化时将数据发送到服务器。

请注意,您需要四处走动才能发生Thread事件。

我假设您将添加代码以将onLocationChanged()发送到jSONString中的服务器,这看起来不错。

首先,修复您的AsyncTask

AsyncTask

class MyAsyncTask extends AsyncTask<String, Integer, Void> { @Override protected Void doInBackground(String... params) { //System.out.println("The output of : doInBackground " +jSONString); System.out.println("The output of : doInBackground " +params[0]); //use the first index of params return null; } } 方法中,添加timer()作为参数,并从构造函数中删除参数:

jSONString

onLocationChanged方法:

public PostData() {
    super();
}

public String getjSONString() {
    return jSONString;
}

public void setjSONString(String jSONString) {
    this.jSONString = jSONString;
}

public void timer(String jSONString) {
     this.jSONString = jSONString; //add this here
     new MyAsyncTask().execute(jSONString);
}

然后在@Override public void onLocationChanged(Location location) { if (location != null) { pLong = location.getLongitude(); pLat = location.getLatitude(); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss", java.util.Locale.getDefault()); formatted = sdf.format(location.getTime()); //add code below: String jSONString= convertToJSON(pLong, pLat, formatted); sender.timer(jSONString); } } 中,只需初始化MainActivity,但不要调用PostData,因为您还没有位置数据:

timer