小编mik*_*ike的帖子

使用Android Retrofit进行POST

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

    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); …
Run Code Online (Sandbox Code Playgroud)

android android-networking android-asynctask androidhttpclient retrofit

23
推荐指数
2
解决办法
5万
查看次数