Android
Using Kotlin
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import kotlinx.coroutines.*
fun main() {
val client = OkHttpClient()
val request = Request.Builder()
.url("https://api.ipoalerts.in/ipos?status=open")
.addHeader("x-api-key", "your_api_key_here")
.build()
runBlocking {
val response = fetchIpoData(client, request)
println(response)
}
}
suspend fun fetchIpoData(client: OkHttpClient, request: Request): String? {
return withContext(Dispatchers.IO) {
try {
val response: Response = client.newCall(request).execute()
if (response.isSuccessful) {
response.body?.string()
} else {
"Request failed with code: ${response.code}"
}
} catch (e: Exception) {
e.printStackTrace()
"Error fetching data"
}
}
}
Using Java
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.ipoalerts.in/ipos?status=open")
.addHeader("x-api-key", "your_api_key_here")
.build();
new Thread(() -> {
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseData = response.body().string();
runOnUiThread(() -> {
// Handle the response
});
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}