我如何在ok http中发布用户名和密码

时间:2017-05-13 05:33:42

标签: php android

我已经使用http将机构发布到服务器现在我想通过okhttp发布正文 我是编程新手所以请简要介绍一下 请将给定代码修改为okkhttp发布请求 请简要描述如何在ok http中发布正文 提前谢谢

login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (emailLogin.getText().toString().length()>3 &&
                    passwordLogin.getText().toString().length()>4){

                emailText=emailLogin.getText().toString();
                passwordText=passwordLogin.getText().toString();

                new AsyncTask<Void, Integer, String>() {

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        //progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    protected String doInBackground(Void... params) {
                        String response;
                        String body="email="+emailText+"&password="+passwordText;
                        response=http.postRequest(HttpConfigs.URL_AUTH,body);
                        return response;
                    }

                    @Override
                    protected void onPostExecute(String response) {
                        super.onPostExecute(response);
                        try {
                            final JSONObject responseJson = new JSONObject(response);
                            remoteMessage=responseJson.getString("message");
                            if (responseJson.getBoolean("result")){
                                showMessage(remoteMessage);
                                JSONObject data = responseJson.getJSONObject("data");
                                Log.d("WaseemTest","Data"+data);
                                 auto login
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                        finish();
                                    }
                                });
                                progressBar.setVisibility(View.INVISIBLE);
                            }else{
                                showMessage(remoteMessage);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onProgressUpdate(Integer... values) {
                        super.onProgressUpdate(values);
                    }
                }.execute();
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码

    RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("somParam", "someValue")
                .build();

        request = new Request.Builder()
                .url(BASE_URL + route)
                .method("POST", RequestBody.create(null, new byte[0]))
                .post(requestBody)
                .build();

有关详细信息,请参阅以下链接 How to use OKHTTP to make a post request?