使用Android Retrofit进行POST

时间:2013-11-05 18:36:20

标签: android android-asynctask android-networking androidhttpclient retrofit

我是Android编程新手并使用Retrofit。我已就这个主题做了大量研究,但未能找到特定于我需求的解决方案。我正在使用我们的API并尝试发出POST请求。我使用以下非改造代码成功实现了这一目标:

    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));
        }

    }
}

现在,我正在尝试使用Retrofit获得相同的结果,但我不确定如何使用回调从我们的API获取JSON(我假设此调用应该异步发生?):

我使用以下方法创建了一个接口:

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

在我的Activity的onCreate方法中实例化RestAdapter等。当用户按下“登录”按钮(输入用户名和密码后),在按钮的onClick方法内调用以下内容:

    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) {

            }
        }
        );

然而,这并没有按预期工作,我真的不认为我正在接近这个。我希望能够对我们的服务器进行POST,该服务器会发回一个关于该用户的JSON格式数据块。如果有人能指出我正确的方向,那将非常感激。提前谢谢

2 个答案:

答案 0 :(得分:21)

Retrofit的一个好处是不必自己解析JSON。你应该有类似的东西:

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
        }
    }
);

User类似于POJO

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

并且返回的JSON具有与User类匹配的字段:

{
  "name": "Bob User",
  "email": "bob@example.com",
  ...
}

如果您需要自定义解析,那么当使用.setConverter(Converter converter)设置REST适配器的转换器时,其位置是:

  

用于对象的序列化和反序列化的转换器。

答案 1 :(得分:8)

似乎Retrofit在@POST@FormUrlEncoded方面存在问题。如果我们执行同步但在异步时失败,则效果很好。

如果@mike问题是反序列化对象,则User类应为

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

     @SerializedName("email")
     String email;
}

接口类

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

或者

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

哪里

public class UserResponse {

@SerializedName("email")
String email;

@SerializedName("name")
String name;

}