How to add parameters to api (http post) using okhttp library in Android

后端 未结 7 1857
不思量自难忘°
不思量自难忘° 2020-11-30 01:33

In my Android application, I am using okHttp library. How can I send parameters to the server(api) using the okhttp library? currently I am using the following code to acces

相关标签:
7条回答
  • 2020-11-30 02:02

    Usually to avoid Exceptions brought about by the code running in UI thread, run the request and response process in a worker thread (Thread or Asynch task) depending on the anticipated length of the process.

        private void runInBackround(){
    
           new Thread(new Runnable() {
                @Override
                public void run() { 
                    //method containing process logic.
                    makeNetworkRequest(reqUrl);
                }
            }).start();
        }
    
        private void makeNetworkRequest(String reqUrl) {
           Log.d(TAG, "Booking started: ");
           OkHttpClient httpClient = new OkHttpClient();
           String responseString = "";
    
           Calendar c = Calendar.getInstance();
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String booked_at = sdf.format(c.getTime());
    
             try{
                 RequestBody body = new FormBody.Builder()
                    .add("place_id", id)
                    .add("booked_at", booked_at)
                    .add("booked_by", user_name.getText().toString())
                    .add("booked_from", lat+"::"+lng)
                    .add("phone_number", user_phone.getText().toString())
                    .build();
    
            Request request = new Request.Builder()
                    .url(reqUrl)
                    .post(body)
                    .build();
    
            Response response = httpClient
                    .newCall(request)
                    .execute();
            responseString =  response.body().string();
            response.body().close();
            Log.d(TAG, "Booking done: " + responseString);
    
            // Response node is JSON Object
            JSONObject booked = new JSONObject(responseString);
            final String okNo = booked.getJSONArray("added").getJSONObject(0).getString("response");
            Log.d(TAG, "Booking made response: " + okNo);
    
            runOnUiThread(new Runnable()
            {
                public void run()
                {
                    if("OK" == okNo){
                        //display in short period of time
                        Toast.makeText(getApplicationContext(), "Booking Successful", Toast.LENGTH_LONG).show();
                    }else{
                        //display in short period of time
                        Toast.makeText(getApplicationContext(), "Booking Not Successful", Toast.LENGTH_LONG).show();
                    }
                }
            });
    
        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        } catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    
    }
    

    I hope it helps someone there.

    0 讨论(0)
提交回复
热议问题