-1

OkHttpClient not work in my mainactivity.java. I want to use AsyncTask but I don't know how can I change it. I want to send

FirebaseInstanceId.getInstance().getToken();
to my server.

There is my code with OkHttpClient. Thank you

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        String token = FirebaseInstanceId.getInstance().getToken();
        registerToken(token);
        Log.d("TOKEN", token);
    }

    private void registerToken(String token){
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("Token", token)
                .build();

        Request request = new Request.Builder()
                .url("http://localhost/register.php")
                .post(body)
                .build();
        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Joe J.
  • 19
  • 1
  • 6
  • 1
    `How Can I change OkHttpClient to AsyncTask`. Impossible. You cannot compare them. They are different things. The first is a http client. The latter a thread. Please tell better what you want. – greenapps Nov 19 '16 at 07:50
  • I want to send this daha FirebaseInstanceId.getInstance().getToken() to my server without OkHttpClient – Joe J. Nov 19 '16 at 08:11
  • And? Any question rephrasing in sight? – greenapps Nov 19 '16 at 08:15
  • Possible duplicate of [AsyncTask Android example](http://stackoverflow.com/questions/9671546/asynctask-android-example) – Chisko Nov 19 '16 at 08:39
  • It is terrible how you formulate your question. `I want to send FirebaseInstanceId.getInstance().getToken(); to my server.'. But you ment: `"After i got a token string with String token = FirebaseInstanceId.getInstance().getToken(); i want to send string token to my server.". So irrelevant where the content came from. You just want to send a string. – greenapps Nov 19 '16 at 09:15
  • That's my fault, I'm new on android. You are right. I want to send a string without OkHttpClient – Joe J. Nov 19 '16 at 09:23

2 Answers2

1

Try this, You'll have to send url to OkHttpAsync. Like new OkHttpAsync().excute(url);

public class OkHttpAsync extends AsyncTask<String, Void, byte[]> {

OkHttpClient client = new OkHttpClient();

@Override
protected byte[] doInBackground(String... params) {



    OkHttpClient client = new OkHttpClient();
    RequestBody body = new FormBody.Builder()
            .add("Token", token)
            .build();

    Request request = new Request.Builder()
            .url(params[0])
            .post(body)
            .build();
    try {
        Response response = client.newCall(request ).execute();
    return response.body().bytes();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
    protected void onPostExecute(byte[] b ) {
        //Handle result here
        super.onPostExecute(playlistListResponse);
    }
}

EDIT

This is how you send url to the OkHttpAsync class and result will in onPostExcute method of OkHttpAsync class.

OkHttpAsync okHttpAsync = new OkHttpAsync();
okHttpAsync.execute("http://localhost/register.php");

EDIT 2.

I see you are having trouble with network request in android. Network request shouldn't be on mainThread. If you fire it on main Thread then it will cause exception. There are many libaraies for network requests in android. i would recommend you use Volley. Here is a tutorial, go through it.

Hope this will help you

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • Thank you @ZeeshanShabbir I can't see url for website on your code. I'm new on android, Can you give me full code, sorry for that – Joe J. Nov 19 '16 at 07:10
  • 1
    OP does not want to use OkHttpClient was what i understood. So i do not understand this answer. – greenapps Nov 19 '16 at 08:19
  • OP didn't represent his question according to his needs. Question needs to go through edit. – Zeeshan Shabbir Nov 19 '16 at 08:23
  • FirebaseInstanceId.getInstance().getToken() this code gives me a token. I want to send this token to my server. I can send token with first code in my answer but I need to send token without OkHttpClient. Because OkHttpClient not work in mymainactivity.java – Joe J. Nov 19 '16 at 08:28
  • I have edited my answer. Please go through the tutorial that i have mentioned. It will solve your problem. – Zeeshan Shabbir Nov 19 '16 at 08:36
  • Thanks for all @ZeeshanShabbir but I don't see FirebaseInstanceId.getInstance().getToken() in your answer – Joe J. Nov 19 '16 at 08:45
  • I want to send this data to my server FirebaseInstanceId.getInstance().getToken() – Joe J. Nov 19 '16 at 08:46
  • I have editted the answer again. Have a look into it – Zeeshan Shabbir Nov 19 '16 at 08:54
0

After you create class mentioned in above answer, just create an object and pass the url as follow:

OkHttpAsync okHttpAsync = new OkHttpAsync();
okHttpAsync.execute("http://localhost/register.php");

This is what @Zeeshan mentioned in first line of his answer.

Thank You.

chetan
  • 681
  • 4
  • 21