Skip to content

Instantly share code, notes, and snippets.

@benjaminsinzore
Created October 26, 2022 13:36
Show Gist options
  • Save benjaminsinzore/898e6c48f99bdad9bc12f6c3cd4e70ca to your computer and use it in GitHub Desktop.
Save benjaminsinzore/898e6c48f99bdad9bc12f6c3cd4e70ca to your computer and use it in GitHub Desktop.

Revisions

  1. benjaminsinzore created this gist Oct 26, 2022.
    24 changes: 24 additions & 0 deletions AndroidManifest.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codinginflow.okhttprequestexample">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>

    </manifest>
    55 changes: 55 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    package com.codinginflow.okhttprequestexample;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;

    import java.io.IOException;

    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;

    public class MainActivity extends AppCompatActivity {

    private TextView mTextViewResult;

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

    mTextViewResult = findViewById(R.id.text_view_result);

    OkHttpClient client = new OkHttpClient();

    String url = "https://reqres.in/api/users?page=2";

    Request request = new Request.Builder()
    .url(url)
    .build();

    client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
    e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
    if (response.isSuccessful()) {
    final String myResponse = response.body().string();

    MainActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    mTextViewResult.setText(myResponse);
    }
    });
    }
    }
    });
    }
    }
    19 changes: 19 additions & 0 deletions activity_main.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.codinginflow.okhttprequestexample.MainActivity">

    <TextView
    android:id="@+id/text_view_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>