Home » Android » My app crashes when using OkHttp

My app crashes when using OkHttp

Posted by: admin November 1, 2017 Leave a comment

Questions:

When I’m trying to send post request using OkHttp, the app on my phone (LG g3) crashes without an error.

btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), "login=test&pasword=test");
            Request request = new Request.Builder()
                    .url("http://myUrl")
                    .post(body)
                    .build();
            try {
                Response response = client.newCall(request).execute();

                txtRequest.setText(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

What am I doing wrong?

Answers:

you are executing the network call on main thread, this is the exception i guess you are getting. Use AsyncTask and your problem is solved. here is corrected version of your code.
Edited

    package com.example.nisu.postrequest;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SendPost extends AppCompatActivity {

    Button btnSend;
    TextView txtRequest;
    OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_post);

        btnSend = (Button) findViewById(R.id.btnSend);
        txtRequest = (TextView) findViewById(R.id.txtRequest);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Request request = new Request.Builder()
                        .url("http://httpbin.org/ip").get()
                        .build();
                new MyAsyncTask().execute(request);
            }
        });
    }

    class MyAsyncTask extends AsyncTask<Request, Void, Response> {

        @Override
        protected Response doInBackground(Request... requests) {
            Response response = null;
            try {
                response = client.newCall(requests[0]).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPostExecute(Response response) {
            super.onPostExecute(response);
            try {
                txtRequest.setText(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}