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
Retrofit automatically serialises the JSON response using a POJO(Plain Old Java Object) which must be defined in advanced for the JSON Structure. To serialise JSON we need a converter to convert it into Gson first. We need to add the following dependencies in our build.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With