-2

i have used http to post body to server now i want to post body through okhttp i am a new in programming so please describe briefly please modify the given code to okkhttp post request please breifly describe how to post body in ok http thanks in advance

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 Answers1

0

Try below code

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

for further information you may refer below link How to use OKHTTP to make a post request?

Community
  • 1
  • 1
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58