Schwartz914
Schwartz914

Reputation: 35

okhttpclient for android and Laravel API

Currently building an Android App that has a Web Service. Trying to get data from the SQL Database using the okhttp3 but I'm getting a weird response and I can't figure it out. My API in Laravel is:

public function getAccount(Request $request, User $user)
    {
      $email = $request->input('email');
      //$response = Users::find($email);
      $response = DB::table('users')->where('email', $email)->first();
      $count = count($response);
      if($count == 0) {
        return Response::json([
          'message' => 'An error occured',
        ], 401);
      } else {
        return Response::json([
          'user' => $response->name,
          'lastName' => $response->lastName,
          'weight' => $response->weight,
          'height' => $response->height,
          'dob' => $response->DOB,
          'email' => $response->email,
        ], 200);
      }

And my Android code is:

 private void getDetails() {
        Thread thread = new Thread(new Runnable(){
            public void run() {

                OkHttpClient client = new OkHttpClient();

                // Json string with email and password
                String bodyString = "{\n\t\"email\" : \"" + email +"\"\n}";

                // Make HTTP POST request
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, bodyString);
                Request request = new Request.Builder()
                        .url("http://192.168.1.100/CAB398/public/api/auth/getAccount")
                        .post(body)
                        .addHeader("accept", "application/json")
                        .addHeader("content-type", "application/json")
                        //.addHeader("cache-control", "no-cache")
                        //.addHeader("postman-token", "c3d60511-7e0f-5155-b5ad-66031ad76578")
                        .build();

                // execute request
                try {
                    Response response = client.newCall(request).execute();

                    String responseData = response.body().toString();

                    // Response code 200 means login details found in DB
                    if(response.code() == 200){
                        etfirstName.setText(responseData);
                     } else if(response.code() == 401){

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
        //wait for thread to finish
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

The issue is that I am getting the response of: okhttp3.internal.http.RealResponseBody@b8a47c8 as the response.body().toString(). I have tested the API call using postman and do get the correct response of:

{"user":"Joe","lastName":"smith","weight":108,"height":179,"dob":"1980-09-06","email":"[email protected]"}

I think I am messing up the Request. Builder but I can't figure this out.

Cheers Peter

Upvotes: 2

Views: 596

Answers (1)

Daniel Bejan
Daniel Bejan

Reputation: 1468

You have mistaken the string() method from the ResponseBody object with the usual toString() method.. Here are some docs for it.

Just have:

String responseData = response.body().string();

instead of:

String responseData = response.body().toString();

You can find some info in here also

Upvotes: 2

Related Questions