1

I have an app on Android that tracks location. I included the tracks in a list object. The issue is, when I send the list to the server, it never gets to the server and from my log, no error is displayed.

Snippet sending to server

public boolean sendTracks() throws    IOException,SocketException,ConnectException{

    String url = "http://192.168.0.180:9080/AddressProcessin/rest/tracks";
    AllTracks alltracks  = new AllTracks();

    //get tracks from sqllite
    alltracks.setTrackList(helper.GetTrackData());

    HttpHeaders requestHeaders = new HttpHeaders();

    requestHeaders.setAccept(Collections.singletonList(new MediaType("application","xml")));

    HttpEntity<AllTracks> requestEntity = new HttpEntity<AllTracks>(alltracks,requestHeaders);

    // Create a new RestTemplate instance

    RestTemplate restTemplate = new RestTemplate();

    ResponseEntity<AllTracks> responseEntity = 
    restTemplate.exchange(url, HttpMethod.POST, requestEntity,     AllTracks.class);

    // get response body 
    AllTracks tracks = responseEntity.getBody();

    return true;            
}

AllTracks class

public class AllTracks implements Serializable{

    private static final long serialVersionUID = -2109883468783032832L;

    //@ElementList(inline=true)
    private List<Tracks> trackList;

    public List<Tracks> getTrackList() {
        return trackList;
    }

    public void setTrackList(List<Tracks> trackList) {
        this.trackList = trackList;
    }    

    public AllTracks() {
        super();
        // TODO Auto-generated constructor stub
    }
}    

Track class

public class Tracks implements Serializable{

    private static final long serialVersionUID = -6318006291038916587L;

    //@Element(required = false)
    @DatabaseField
    private Date dateCaptured;


    //@Element
    @DatabaseField
    private double longitude;

    //@Element
    @DatabaseField
    private double latitude;

    //@Element
    @DatabaseField
    private long userId;

    public double getLongitude() {
        return longitude;
    }    
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }    

    public double getLatitude() {
        return latitude;
    }    

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    // getters and setters 
    public Date getDateCaptured() {
        return dateCaptured;
    }

    public void setDateCaptured(Date dateCaptured) {
        this.dateCaptured = dateCaptured;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    // contructor 
    public Tracks(Date dateCaptured, long userId) {
        super();
        this.dateCaptured = dateCaptured;
        this.userId = userId;
    }
    public Tracks(Date dateCaptured, double longitude, double latitude,
        long userId) {
        super();
        this.dateCaptured = dateCaptured;
        this.longitude = longitude;
        this.latitude = latitude;
        this.userId = userId;
    }
    public Tracks() {
        super();
        // TODO Auto-generated constructor stub
    }
}
Opal
  • 81,889
  • 28
  • 189
  • 210
mikeb
  • 21
  • 1
  • Might wanna tidy up your code blocks, missing half of two and the entire of three. Welcome to SO! :) – Aphire May 28 '15 at 15:31
  • What is the response `statusCode`? how do you now the request never reached the server? –  May 28 '15 at 15:46
  • do you actually log the exceptions thrown by `sendTracks`? – njzk2 May 28 '15 at 15:50
  • @RC the code do not get past this line ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, AllTracks.class); – mikeb May 29 '15 at 20:32
  • @njzk2 I do log the exceptions but strangely, none is thrown – mikeb May 29 '15 at 20:34

1 Answers1

0

Are you using AsyncTask to send/receive your request over the network? If not ... you have to do so! There are a lot of examples of how to use AsyncTask in Android.

check out this link AsyncTask Android example

Community
  • 1
  • 1
kamokaze
  • 7,142
  • 5
  • 33
  • 43