0

I'm a litte bit frustrated. I've got a problem. I'm trying to send all inputs of a user via put REST request to the server. This is how the request json object look on the server:

{
    id: "123",
    text: "My Name is Peter...,
    age": 15,
    name: "Peter",
    hobbies: [                 
            id": 321,
            hobbie_id": 1,
            name": "Football",
            },               
            id": 213,
            hobbie_id": 1,
            name": "Basketball",
            }      
    ],
    gender: true,
    version: 1
}

I have a Class with a inner Class which extends from AsyncTask to handle the server communication in background:

public class MyActivity extends Activity{

    private class PutServiceTask extends AsyncTask<String, Integer, String> {


        @Override
        protected void onPreExecute() {
            //show Dialogbox
        }

        @Override
        protected String doInBackground(String... urls) {
            String url = urls[0];
            String result = "";
            HttpResponse response = doResponse(url);

            if (response == null) {
                return result;
            }

            return result;

            private HttpResponse doResponse(String url) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPut httpput = new HttpPut(url);
                HttpResponse response = null;

                // Add parameters
                try {
                    response = httpclient.execute(httpput);
                    StatusLine statusLine = response.getStatusLine();
                    int statusCode = statusLine.getStatusCode();
                    if(statusCode == HttpStatus.SC_OK){ //200
                        httpput.addHeader("content-type", "application/json");
                        StringEntity se = new StringEntity(params.toString());
                        httpput.setEntity(se);
                        HttpEntity entity = response.getEntity();
                    }
                    else {
                        System.out.println(response.getStatusLine().getReasonPhrase());
                    }

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


                return response;
            }   
        }

    }
}

And the Questions now is how to put this on the server in the onPostExecute Methode.These are my user inputs which I get from i.e. EditText and converted toString:

private String ageString; //age
private String nameString; //name of the Person
private String hobbyname; //name of the Hobby
private String textString; //text

An the other thing is the ID. Is this correct that the server generates the id, right?

I hope somebody can help me in this case!

thanks in advance.

user2511241
  • 1
  • 1
  • 2

1 Answers1

0

Well this is too complex to handle it yourself, take a look at this library with much cleaner API and type-safe data.

https://github.com/kodart/Httpzoid

Here is a simple usage example

Http http = HttpFactory.create(context);
http.post("http://example.com/users")
    .data(new User("John"))
    .execute();

Or more complex with callbacks

Http http = HttpFactory.create(context);
http.post("http://example.com/users")
    .data(new User("John"))
    .handler(new ResponseHandler<Void>() {
        @Override
        public void success(Void ignore, HttpResponse response) {
        }

        @Override
        public void error(String message, HttpResponse response) {
        }

        @Override
        public void failure(NetworkError error) {
        }

        @Override
        public void complete() {
        }
    }).execute();

It is fresh new, but looks very promising.

Arthur
  • 674
  • 1
  • 8
  • 14