home / skills / pluginagentmarketplace / custom-plugin-android / networking

networking skill

/skills/networking

This skill helps you implement robust network communication using Retrofit and OkHttp with secure, scalable error handling and data serialization.

npx playbooks add skill pluginagentmarketplace/custom-plugin-android --skill networking

Review the files below or copy the command above to add this skill to your agents.

Files (13)
SKILL.md
2.2 KB
---
name: networking
description: Retrofit, OkHttp, REST APIs, JSON serialization, network security.
version: "2.0.0"
sasmp_version: "1.3.0"

# Agent Binding
bonded_agent: 05-networking
bond_type: PRIMARY_BOND

# Skill Configuration
atomic: true
single_responsibility: HTTP networking & API integration

# Parameter Validation
parameters:
  api_type:
    type: string
    enum: [rest, graphql]
    default: rest
  concern:
    type: string
    enum: [setup, error_handling, security, caching]
    required: false

# Retry Configuration
retry:
  max_attempts: 3
  backoff: exponential
  on_failure: provide_fallback_pattern

# Observability
logging:
  level: info
  include: [api_endpoint, response_code, latency]
---

# API Integration Skill

## Quick Start

### Retrofit Setup
```kotlin
interface UserApi {
    @GET("/users/{id}")
    suspend fun getUser(@Path("id") id: Int): UserDto
    
    @POST("/users")
    suspend fun createUser(@Body user: UserDto): UserDto
}

val retrofit = Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build()

val api = retrofit.create(UserApi::class.java)
```

### OkHttp Configuration
```kotlin
val client = OkHttpClient.Builder()
    .addInterceptor(HttpLoggingInterceptor())
    .connectTimeout(30, TimeUnit.SECONDS)
    .certificatePinner(CertificatePinner.Builder()
        .add("api.example.com", "sha256/...").build())
    .build()
```

### Error Handling
```kotlin
sealed class Result<T> {
    data class Success<T>(val data: T) : Result<T>()
    data class Error<T>(val exception: Exception) : Result<T>()
}
```

## Key Concepts

### HTTP Methods
- GET: Fetch data
- POST: Create resource
- PUT/PATCH: Update
- DELETE: Remove

### Retrofit Features
- Type-safe interfaces
- Automatic serialization
- Suspend function support
- Error callbacks

### Network Security
- HTTPS/TLS enforcement
- SSL pinning
- Certificate validation
- Secure token storage

## Best Practices

✅ Use HTTPS always
✅ Implement SSL pinning
✅ Handle errors gracefully
✅ Optimize request/response size
✅ Cache when possible

## Resources

- [Retrofit Guide](https://square.github.io/retrofit/)
- [OkHttp Documentation](https://square.github.io/okhttp/)

Overview

This skill helps Android developers implement robust networking using Retrofit and OkHttp, including JSON serialization and network security patterns. It focuses on type-safe API interfaces, efficient HTTP client configuration, and secure transport practices to reduce runtime errors and vulnerabilities. Use it to standardize API calls, error handling, and performance tuning in mobile apps.

How this skill works

The skill inspects common networking tasks: defining Retrofit service interfaces, configuring OkHttp clients, and setting up JSON converters (Gson, Moshi). It highlights interceptors, timeouts, certificate pinning, and secure token management to enforce HTTPS and validate certificates. Error handling patterns and result wrappers are provided to surface success and failure states cleanly to callers.

When to use it

  • Building or refactoring Android REST API clients with Retrofit
  • Configuring OkHttp for logging, timeouts, and certificate pinning
  • Adding JSON serialization and mapping between DTOs and domain models
  • Implementing centralized error handling and retry policies
  • Hardening app networking with HTTPS and secure token storage

Best practices

  • Always use HTTPS and validate certificates; prefer certificate pinning for high-risk apps
  • Centralize API definitions with Retrofit interfaces and use suspend functions for coroutines
  • Use a JSON converter (Gson/Moshi) and map DTOs to domain models to isolate parsing logic
  • Add OkHttp interceptors for logging, authentication headers, and performance metrics
  • Return a sealed Result type or wrapper to unify success and error flow across layers
  • Cache responses and compress payloads to reduce bandwidth and improve perceived performance

Example use cases

  • Define a UserApi Retrofit interface and implement get/create endpoints with suspend functions
  • Configure OkHttp with logging, 30s timeouts, and a certificate pinner for production builds
  • Wrap network calls in a Result sealed class and handle exceptions at the repository layer
  • Switch JSON converters or add custom adapters to handle dates and polymorphic types
  • Implement token refresh with an OkHttp authenticator and retry failed requests automatically

FAQ

How do I choose between Gson and Moshi?

Choose Moshi for modern features and smaller footprints; use Gson if you rely on legacy behavior or existing code. Both integrate with Retrofit via converter factories.

When should I enable SSL pinning?

Enable pinning for high-security apps (finance, health) to reduce MITM risk, but plan for certificate rotation and fallback to avoid locking out users.