POST with Android Retrofit
Asked Answered
M

2

23

I'm new to Android programming and working with Retrofit. I've done a bunch of research on this topic, but haven't been able to find a solution specific to my needs. I'm working with our API and trying to make a POST request. I successfully achieved this with the following non-Retrofit code:

    private class ProcessLogin extends AsyncTask<Void, String, JSONObject> {
    private ProgressDialog pDialog;
    String email,password;

    protected void onPreExecute() {
        super.onPreExecute();
        inputEmail = (EditText) findViewById(R.id.email);
        inputPassword = (EditText) findViewById(R.id.password);
        email = inputEmail.getText().toString();
        password = inputPassword.getText().toString();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Logging in ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected JSONObject doInBackground(Void... params) {
        HttpURLConnection connection;
        OutputStreamWriter request = null;
        URL url = null;   
        String response = null;         
        String parameters = "username="+email+"&password="+password;  
        try
        {
            url = new URL("http://.../api/login");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");    

            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();            
            String line = "";               
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            // Response from server after login process will be stored in response variable.                
            response = sb.toString();
            // You can perform UI operations here
            isr.close();
            reader.close();
        }
        catch(IOException e)
        {
            // Error
        }
        System.out.println(response);
        JSONObject jObj = null;        
        // Try to parse the string to a JSON Object
        try {
            jObj = new JSONObject(response);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // Return the JSONObject
        return jObj;
    }

    protected void onPostExecute(JSONObject json) {
        String status = (String) json.optJSONArray("users").optJSONObject(0).optJSONObject("user").optString("login");
        pDialog.dismiss();
        if (status.equals("Failed")) {
            loginMessage.setText("Login Failed");   
        }
        else if (status.equals("Success")) {
            loginMessage.setText("Success!");   
            startActivity(new Intent(getApplicationContext(), DActivity.class));
        }

    }
}

Now, I'm trying to get the same results using Retrofit, but I'm not sure how to get the JSON back from our API using Callbacks (I'm assuming this call should happen asynchronously?):

I've made an interface with the following method:

    @FormUrlEncoded
@POST("/login")
public void login(@Field("username") String username, @Field("password") String password, Callback<JSONObject> callback);

And instantiated the RestAdapter, etc in my Activity's onCreate method. When the user presses the "login" button (after entering their username and password), the following is called inside the button's onClick method:

    service.login(email, password, new Callback<JSONObject>() { 
            @Override
            public void failure(final RetrofitError error) {
                android.util.Log.i("example", "Error, body: " + error.getBody().toString());
            }
            @Override
            public void success(JSONObject arg0, Response arg1) {

            }
        }
        );

This is not working as intended however, and I really don't think I'm approaching this correctly. I want to be able to make a POST to our server, which sends back a block of JSON formatted data about that user. If anyone could point me in the right direction, that would be really appreciated. Thank you in advance

Mccaslin answered 5/11, 2013 at 18:36 Comment(5)
In which android API you are working?Couloir
@BlueGreen im working with my company's own API, so I'm trying to use Retrofit to make GET/POST callsMccaslin
I'm testing my REST with Retrofit right now, doing the same thing as you, but I'm not yet ready to give you a full answer. For starters try using Callback<Response> - that should give you a raw data. Your code seems to look correct in generalMenefee
I'm currently facing the same problem. Did you manage to get it working?Mcmullin
Did you get it to work? I'm doing the same thing and I'm getting an IllegalArgumentException in trying to do an asynchronous POST with a callback which is FormUrlEncoded - #22572801Genovera
K
22

One of the benefits of Retrofit is not having to parse the JSON yourself. You should have something like:

service.login(email, password, new Callback<User>() { 
        @Override
        public void failure(final RetrofitError error) {
            android.util.Log.i("example", "Error, body: " + error.getBody().toString());
        }
        @Override
        public void success(User user, Response response) {
            // Do something with the User object returned
        }
    }
);

Where User is a POJO like

public class User {
    private String name;
    private String email;
    // ... etc.
}

and the returned JSON has fields that match the User class:

{
  "name": "Bob User",
  "email": "[email protected]",
  ...
}

If you need custom parsing, the place for that is when setting the REST adapter's converter with .setConverter(Converter converter):

The converter used for serialization and deserialization of objects.

Knipe answered 6/12, 2013 at 0:55 Comment(4)
Excellent example with all "parts" of the equation clearly demonstrated. Nice one.Berte
what about if we get json array '[' instead of Objects '{' ? [ "name": "Bob User", "email": "[email protected]", ... ]Untruth
For arrays you can use List<User>Anabasis
@Untruth Send it back, that's not JSON.Knipe
O
8

Seem Retrofit have problem with @POST and @FormUrlEncoded. It works well if we do synchronous but failed if asynchronous.

If @mike problem is deserialization object, User class should be

public class User {
     @SerializedName("name")
     String name;

     @SerializedName("email")
     String email;
}

Interface class

@FormUrlEncoded
@POST("/login")
void login(@Field("username") String username, @Field("password") String password, Callback<User> callback);

Or

@FormUrlEncoded
@POST("/login")
void login(@Field("username") String username, @Field("password") String password, Callback<UserResponse> callback);

Where

public class UserResponse {

@SerializedName("email")
String email;

@SerializedName("name")
String name;

}
Older answered 13/7, 2015 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.