【问题标题】:Android - Rest Post CallAndroid - 休息后通话
【发布时间】:2015-03-09 19:16:46
【问题描述】:

我在调用 rest api 时遇到了一些麻烦。使用 Chrome Advanced Rest Client 时,我使用 URL:http://example.com/task_manager/v1/register 进行测试,效果很好。我试图让用户能够通过 android 注册。当我单击我的按钮时,我没有看到对数据库的任何更改。

我是通过xml调用方法android:onClick="register"

public class LoginActivity extends Activity {
    public EditText enterEmail;
    public EditText enterPassword;
    public EditText forgotPassword;
    public ImageButton loginButton;
    public ImageButton RegisterButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //hide status bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.login_activity);
        enterEmail = (EditText) findViewById(R.id.emailentry);
        enterPassword = (EditText) findViewById(R.id.passwordentry);
        forgotPassword = (EditText) findViewById(R.id.forgotpass);
        //login
        loginButton = (ImageButton) findViewById(R.id.loginbutton);

        //register
        RegisterButton = (ImageButton) findViewById(R.id.registerbutton);

    }

    public void login(View view){

    }

    public void register(View view){
        String email = enterEmail.getText().toString();
        String password = enterPassword.getText().toString();
        String name = "ANDROID";
        String apiURL = "http://example.com/task_manager/v1/register";

        if( (email != null && !email.isEmpty()) && (password != null && !password.isEmpty()) && (name != null && !name.isEmpty())) {

            String urlString = apiURL + "name="+name+"&email="+email+"&password="+password;

            new CallAPI().execute(urlString);

        }
    }



    private class CallAPI extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            String urlString=params[0]; // URL to call

            String resultToDisplay = "";

            InputStream in = null;

            // HTTP Get
            try {

                URL url = new URL(urlString);

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                in = new BufferedInputStream(urlConnection.getInputStream());

            } catch (Exception e ) {

                System.out.println(e.getMessage());

                return e.getMessage();

            }

            return resultToDisplay;
        }

        protected void onPostExecute(String result) {

        }

    } // end CallAPI
}

【问题讨论】:

    标签: android rest http post


    【解决方案1】:

    对于附加请求参数,结束 url 不应如下所示:

    String apiURL = "http://example.com/task_manager/v1/register?";

    【讨论】:

      【解决方案2】:

      如果您的问题的重点是发出 POST 请求,您需要在调用 getInputStream 之前设置请求方法。

      调用 url.openConnection() 后,需要调用 urlConnection.setRequestMethod("POST")。

      正如 Jayesh 所说,您的网址似乎也不正确。

      【讨论】:

        【解决方案3】:

        我通过重写类以使用 JSON 解析器解决了这个问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-07
          • 1970-01-01
          • 2020-02-15
          • 1970-01-01
          • 2018-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多