Pergunta

I need to send some data from my Android device to my server. I am doing this through JSON. I have implemented the JSON post on Android, and I am trying to do a mapping on the server side in order to retrieve that data. My problem is that I keep getting an empty string.

Android method used to send JSON:

private void sendJson(final String json, final String URL) {
    Thread t = new Thread(){
    public void run() {
            Looper.prepare(); //For Preparing Message Pool for the child Thread
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
            try{
                HttpPost post = new HttpPost(URL);
                StringEntity se = new StringEntity(json);  
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);
                client.execute(post);
            }
            catch(Exception e){
                e.printStackTrace();
            }
            Looper.loop(); //Loop in the message queue
        }
    };
    t.start();      
}

Server-side method:

@RequestMapping(value = "/getLatestCalls", method = RequestMethod.POST)
public void getData(@ModelAttribute String json){
    //... do something 
}

The thing is that in this method my json String is "" every time. I have also tried using @RequestParam but with that it doesn't enter the method anymore. I have also tried with @ModelAttribute("json").

Can someone enlighten me a little here? Thank you in advance.

Foi útil?

Solução

Here is the solution and it works fine.

server-side

@Controller
    public class DataCollector {

        @RequestMapping(value = "/clientdatacollector", method = RequestMethod.POST)
        public @ResponseBody
        void abc(Writer writer, @RequestParam("gpsdata") String gpsJSON) {



            try {
                // here is your jsonstring ;)
                writer.write(gpsJSON.toString());

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

client-side

 public static void httptest() {
            ArrayList<TravellingData> tdArray = new ArrayList<TravellingData>();
            Gson gson = new Gson();
            String jsonString = "";
            for (int i = 0; i < 1; i++) {
                tdArray.add(ObjectCreater.createMockTravellingDataObject());
            }

            jsonString = gson.toJson(tdArray);

            HttpClient client = new DefaultHttpClient();

            HttpPost post = null;
            try {
                post = new HttpPost(
                        "http://localhost:8080/uygulama/clientdatacollector");
            } catch (URISyntaxException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("gpsdata", jsonString));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = null;
                try {
                    response = client.execute(post);
                } catch (HttpException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                    System.out.println(line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

Outras dicas

Try using @RequestBody. It should work.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top