Android: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(29 intermediate revisions by the same user not shown)
Line 7: Line 7:
   @Get("/events/{year}/{location}")
   @Get("/events/{year}/{location}")
   EventList getEventsByLocationAndYear(@Path String location, @Path int year);
   EventList getEventsByLocationAndYear(@Path String location, @Path int year);
}
</source>
==Dependencies==
<source lang="xml">
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>org.androidannotations</groupId>
    <artifactId>rest-spring</artifactId>
    <version>4.8.0</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    <scope>provided</scope>
</dependency>
</source>
==Delete with Body==
<source lang="java">
package org.chorke.academia.utility;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import java.io.IOException;
import java.net.HttpURLConnection;
public class RestClientFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        super.prepareConnection(connection, httpMethod);
        if("DELETE".equals(httpMethod)) {
            connection.setDoOutput(true);
        }
    }
}
</source>
==Rest Implement==
<source lang="java">
package org.chorke.academia.beans.endpoint;
import org.chorke.academia.utility.RestClientFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import java.util.Map;
public interface ProductEndpoint {
    String ROOT_ENDPOINT = "https://dev.chorke.org/academia/api/rest/v1.0";
    //@Get("/product")
    Map<String, Object> getAll();
    //@Get("/product/{id}")
    Map<String, Object> getOne(int id);
    //@Post("/product")
    Map<String, Object> create(Map<String, Object> product);
    //@Put("/product/{id}")
    Map<String, Object> update(int id, Map<String, Object> product);
    //@Delete("/delete/{id}")
    Map<String, Object> delete(int id, Map<String, Object> product);
    enum Actions {
        GET_PRODUCTS("/product"),
        GET_PRODUCT("/product/{id}"),
        POST_PRODUCT("/product"),
        PUT_PRODUCT("/product/{id}"),
        DELETE_PRODUCT("/product/{id}");
        private String uri;
        Actions(String uri){
            this.uri = uri;
        }
        public String getUri() {
            return uri;
        }
    }
    default RestTemplate getEndpoint() {
        RestTemplate endpoint = new RestTemplate(new RestClientFactory());
        endpoint.setUriTemplateHandler(new DefaultUriBuilderFactory(this.getRootUri()));
        return endpoint;
    }
    RestTemplateBuilder getBuilder();
    String getRootUri();
}
</source>
<source lang="java">
package org.chorke.academia.beans.endpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.HashMap;
import java.util.Map;
@Component
public class ProductEndpointImpl implements ProductEndpoint {
    @Value("${chorke.academia.endpoint.root.uri:}")
    String rootUri;
    @Autowired
    RestTemplateBuilder builder;
    //@Get("/product")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getAll(){
        String uri = UriComponentsBuilder.fromUriString(Actions.GET_PRODUCTS.getUri())
                .queryParam("sort","code")
                .queryParam("dir","ASC")
                .queryParam("limit",10)
                .queryParam("start",0)
                .queryParam("page",1)
                .build().toUriString();
        return this.getEndpoint()
                .getForObject(uri, Map.class);
    }
    //@Get("/product/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getOne(int id){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return this.getEndpoint()
                .getForObject(
                        Actions.GET_PRODUCT.getUri(),
                        Map.class, params
                );
    }
    //@Post("/product")
    @SuppressWarnings("unchecked")
    public Map<String, Object> create(Map<String, Object> product){
        return this.getEndpoint()
                .postForObject(
                        Actions.POST_PRODUCT.getUri(),
                        product, Map.class
                );
    }
    //@Put("/product/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(int id, Map<String, Object> product){
        HttpEntity<Map<String, Object>> payload = new HttpEntity<>(product);
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return this.getEndpoint()
                .exchange(
                        Actions.PUT_PRODUCT.getUri(),
                        HttpMethod.PUT, payload,
                        Map.class,
                        params
                ).getBody();
    }
    //@Delete("/product/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> delete(int id, Map<String, Object> product){
        HttpEntity<Map<String, Object>> payload = new HttpEntity<>(product);
        Map<String, Object> params = new HashMap<>();
        params.put("id"  , id);
        return this.getEndpoint()
                .exchange(
                        Actions.DELETE_PRODUCT.getUri(),
                        HttpMethod.DELETE, payload,
                        Map.class,
                        params
                ).getBody();
    }
    @Override
    public RestTemplateBuilder getBuilder() {
        return builder;
    }
    @Override
    public String getRootUri() {
        rootUri = !StringUtils.isEmpty(rootUri) ? rootUri : ROOT_ENDPOINT;
        return rootUri;
    }
}
}
</source>
</source>
Line 17: Line 217:
  '''compile''' 'org.jetbrains.anko:anko-design:0.8.3'
  '''compile''' 'org.jetbrains.anko:anko-design:0.8.3'
  '''compile''' 'org.jetbrains.anko:anko-appcompat-v7:0.8.3'
  '''compile''' 'org.jetbrains.anko:anko-appcompat-v7:0.8.3'
==Knowledge==
%APPDATA%\..\Local\Android\sdk
<source lang="powershell">
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
#Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
</source>


==References==
==References==
{|
| valign="top" |
* [https://github.com/google/android-emulator-hypervisor-driver-for-amd-processors/issues/17 Android Emulator Hypervisor Driver for AMD CPU]
* [https://stackoverflow.com/questions/11046258 What is Coredata equivalent for Android]
* [https://stackoverflow.com/questions/11046258 What is Coredata equivalent for Android]
* [https://stackoverflow.com/questions/44342809/ Android + AndroidAnnotations REST API]
* [https://www.sitepoint.com/building-a-ui-with-kotlin-and-anko/ Building a UI with Kotlin and Anko]
* [https://www.sitepoint.com/building-a-ui-with-kotlin-and-anko/ Building a UI with Kotlin and Anko]
* [https://github.com/androidannotations/androidannotations/wiki/Rest-API Android Annotations Rest API]
* [https://github.com/androidannotations/androidannotations/wiki/Rest-API Android Annotations Rest API]
* [https://github.com/androidannotations/androidannotations/wiki Android Annotations Wiki]
* [https://github.com/androidannotations/androidannotations/wiki Android Annotations Wiki]
* [http://androidannotations.org/ Android Annotations]
* [http://androidannotations.org/ Android Annotations]
* [https://quarkus.io/guides/rest-client Quarkus Rest Client]
* [https://github.com/Kotlin/anko/wiki/Anko-Layouts Anko Layouts]
* [[Lombok]]
| valign="top" |
* [https://stackoverflow.com/questions/2237803/ Method parameter name using Java Reflection]
* [https://www.baeldung.com/spring-rest-template-multipart-upload Spring RestTemplate Upload MultipartFile]
* [https://www.baeldung.com/spring-cloud-netflix-hystrix A Guide to Spring Cloud Netflix-Hystrix]
* [https://www.baeldung.com/spring-rest-template-error-handling Spring RestTemplate Error Handling]
* [https://dev.to/habeebcycle/spring-value-annotation-tricks-1a80 Spring <code>@Value</code> annotation tricks]
* [https://www.eclipse.org/community/eclipse_newsletter/2019/september/autowire_microprofile.php Autowire MicroProfile Into Spring]
* [https://www.oracle.com/java/technologies/java-se-support-roadmap.html Oracle Java SE Support Roadmap]
* [https://stackoverflow.com/questions/8181768/ Can I set a TTL for <code>@Cacheable</code>]
* [https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/ Spring Rest Template]
|}

Latest revision as of 08:17, 25 August 2021

@Rest(rootUrl = "https://api.chorke.org/rest/api", converters = { MappingJackson2HttpMessageConverter.class })
public interface EventsApi {
  @Get("/events/{year}/{location}")
  EventList getEventsByYearAndLocation(@Path int year, @Path String location);

  @Get("/events/{year}/{location}")
  EventList getEventsByLocationAndYear(@Path String location, @Path int year);
}

Dependencies

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.2</version>
</dependency>
<dependency>
    <groupId>org.androidannotations</groupId>
    <artifactId>rest-spring</artifactId>
    <version>4.8.0</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    <scope>provided</scope>
</dependency>

Delete with Body

package org.chorke.academia.utility;

import org.springframework.http.client.SimpleClientHttpRequestFactory;

import java.io.IOException;
import java.net.HttpURLConnection;

public class RestClientFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        super.prepareConnection(connection, httpMethod);
        if("DELETE".equals(httpMethod)) {
            connection.setDoOutput(true);
        }
    }
}

Rest Implement

package org.chorke.academia.beans.endpoint;

import org.chorke.academia.utility.RestClientFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;

import java.util.Map;

public interface ProductEndpoint {
    String ROOT_ENDPOINT = "https://dev.chorke.org/academia/api/rest/v1.0";

    //@Get("/product")
    Map<String, Object> getAll();

    //@Get("/product/{id}")
    Map<String, Object> getOne(int id);

    //@Post("/product")
    Map<String, Object> create(Map<String, Object> product);

    //@Put("/product/{id}")
    Map<String, Object> update(int id, Map<String, Object> product);

    //@Delete("/delete/{id}")
    Map<String, Object> delete(int id, Map<String, Object> product);

    enum Actions {
        GET_PRODUCTS("/product"),
        GET_PRODUCT("/product/{id}"),
        POST_PRODUCT("/product"),
        PUT_PRODUCT("/product/{id}"),
        DELETE_PRODUCT("/product/{id}");

        private String uri;
        Actions(String uri){
            this.uri = uri;
        }

        public String getUri() {
            return uri;
        }
    }

    default RestTemplate getEndpoint() {
        RestTemplate endpoint = new RestTemplate(new RestClientFactory());
        endpoint.setUriTemplateHandler(new DefaultUriBuilderFactory(this.getRootUri()));
        return endpoint;
    }

    RestTemplateBuilder getBuilder();
    String getRootUri();
}
package org.chorke.academia.beans.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.util.Map;

@Component
public class ProductEndpointImpl implements ProductEndpoint {
    @Value("${chorke.academia.endpoint.root.uri:}")
    String rootUri;

    @Autowired
    RestTemplateBuilder builder;

    //@Get("/product")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getAll(){
        String uri = UriComponentsBuilder.fromUriString(Actions.GET_PRODUCTS.getUri())
                .queryParam("sort","code")
                .queryParam("dir","ASC")
                .queryParam("limit",10)
                .queryParam("start",0)
                .queryParam("page",1)
                .build().toUriString();

        return this.getEndpoint()
                .getForObject(uri, Map.class);
    }

    //@Get("/product/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> getOne(int id){
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return this.getEndpoint()
                .getForObject(
                        Actions.GET_PRODUCT.getUri(),
                        Map.class, params
                );
    }

    //@Post("/product")
    @SuppressWarnings("unchecked")
    public Map<String, Object> create(Map<String, Object> product){
        return this.getEndpoint()
                .postForObject(
                        Actions.POST_PRODUCT.getUri(),
                        product, Map.class
                );
    }

    //@Put("/product/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(int id, Map<String, Object> product){
        HttpEntity<Map<String, Object>> payload = new HttpEntity<>(product);
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);

        return this.getEndpoint()
                .exchange(
                        Actions.PUT_PRODUCT.getUri(),
                        HttpMethod.PUT, payload,
                        Map.class,
                        params
                ).getBody();
    }

    //@Delete("/product/{id}")
    @SuppressWarnings("unchecked")
    public Map<String, Object> delete(int id, Map<String, Object> product){
        HttpEntity<Map<String, Object>> payload = new HttpEntity<>(product);
        Map<String, Object> params = new HashMap<>();
        params.put("id"   , id);

        return this.getEndpoint()
                .exchange(
                        Actions.DELETE_PRODUCT.getUri(),
                        HttpMethod.DELETE, payload, 
                        Map.class,
                        params
                ).getBody();
    }

    @Override
    public RestTemplateBuilder getBuilder() {
        return builder;
    }

    @Override
    public String getRootUri() {
        rootUri = !StringUtils.isEmpty(rootUri) ? rootUri : ROOT_ENDPOINT;
        return rootUri;
    }
}

Redmi Note 5

Settings » About Phone » MIUI Version (TAP 7 more Times)
Settings » Additional Settings » Scroll Down » Developer Options

Anko Layout

compile 'org.jetbrains.anko:anko-design:0.8.3'
compile 'org.jetbrains.anko:anko-appcompat-v7:0.8.3'

Knowledge

%APPDATA%\..\Local\Android\sdk
Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
#Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All

References