Snippets

A5.ua Mobile Development Networking and more

Created by Ольга Корохина
@GET("regions/buildingsbyvenue")
Call<CListResponse<V1.Region>> getRegionsByBuilding(@Query("venue_id") long venueId, @Query("region_name") String regionName);
-------------------------------------------------------------------------------------------------------------------------------
public class CListResponse<T> extends CResponse {

    @Expose
    @SerializedName("list")
    private List<T> list;

    public List<T> getList() {
        return list;
    }
}
-------------------------------------------------------------------------------------------------------------------------------
private Call<CListResponse<V1.Region>> generateGetRegionsCall(CBuilding building, String regionName) {
        return module.getRegionsByBuilding(building.getServerId(), regionName);
}

//Sync
public List<CRegion> get(@NonNull CBuilding building, String regionName, boolean autoFloorCreation) {
        try {
            Response<CListResponse<V1.Region>> response = generateGetRegionsCall(building, regionName).execute();
            CListResponse<V1.Region> body = response.body();
            if (body != null) {
                List<V1.Region> regionsList = body.getList();
                if (regionsList != null)
                    return fillRegionsList(building, regionsList, autoFloorCreation);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
}

//Async
public void get(@NotNull final CBuilding building, @Nullable final String regionName, final Callback<List<CRegion>> callback) {
        generateGetRegionsCall(building, regionName).enqueue(new retrofit2.Callback<CListResponse<V1.Region>>() {
            @Override
            public void onResponse(@NonNull Call<CListResponse<V1.Region>> call, @NonNull Response<CListResponse<V1.Region>> response) {
                CListResponse<V1.Region> body = response.body();
                List<CRegion> regions = null;
                if (body != null) {
                    List<V1.Region> regionsList = body.getList();
                    if (regionsList != null)
                        regions = fillRegionsList(building, regionsList, false);
                }
                callback.success(regions);
            }

            @Override
            public void onFailure(@NonNull Call<CListResponse<V1.Region>> call, @NonNull Throwable t) {
                callback.failure(t);
            }
        });
}

@Multipart
@POST("locations/image360Degree")
Call<CreateImage360DegreeResponse> createImage360Degree(@Part MultipartBody.Part filePart,
                                                            @Part("photo_string") RequestBody photoString,
                                                            @Part("image_id") RequestBody imageId,
                                                            @Part("region_id") RequestBody regionId,
                                                            @Part("lat") RequestBody lat,
                                                            @Part("lng") RequestBody lng,
                                                            @Part("rotation") RequestBody rotation,
                                                            @Part("url") RequestBody url);
----------------------------------------------------------------------------------------------------------------														
private Call<IBaseModule.CreateImage360DegreeResponse> generateCreateImage360DegreeCall(CImage360Degree image) {
            File file = new File(image.getLocation());

            RequestBody requestFile =
                    RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part filePart =
                    MultipartBody.Part.createFormData("file", file.getName(), requestFile);

            String string = image.getImageString();
            RequestBody imageString =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), string != null ? string : "");

            RequestBody imageId =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), String.valueOf(image.getFloor().getServerId()));

            CRegion region = image.getRegion();
            RequestBody regionId =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), String.valueOf(region == null ? 0 : region.getServerId()));

            RequestBody latitude =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), String.valueOf(image.getPosition().latitude));

            RequestBody longitude =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), String.valueOf(image.getPosition().longitude));

            RequestBody rotation =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), String.valueOf(image.getRotation()));

            RequestBody url =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), String.valueOf(image.getUrl()));

            return module.createImage360Degree(filePart,
                    imageString,
                    imageId,
                    regionId,
                    latitude,
                    longitude,
                    rotation,
                    url);
}


public Pair<Long, String> createImage360Degree(CImage360Degree image) {
        try {
            Response<IBaseModule.CreateImage360DegreeResponse> response = generateCreateImage360DegreeCall(image, false).execute();
            IBaseModule.CreateImage360DegreeResponse body = response.body();
            if (body != null) {
                long id = body.getId();
                String url = body.getUrl();
                return Pair.create(id, url);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


public void createImage360Degree(CImage360Degree image, final Callback<Pair<Long, String>> callback) {
        generateCreateImage360DegreeCall(image, false).enqueue(new retrofit2.Callback<IBaseModule.CreateImage360DegreeResponse>() {
            @Override
            public void onResponse(@NonNull Call<IBaseModule.CreateImage360DegreeResponse> call, @NonNull Response<IBaseModule.CreateImage360DegreeResponse> response) {
                IBaseModule.CreateImage360DegreeResponse body = response.body();
                Pair<Long, String> result = null;
                if (body != null) {
                    long id = body.getId();
                    String url = body.getUrl();
                    result = Pair.create(id, url);
                }
                callback.success(result);
            }

            @Override
            public void onFailure(@NonNull Call<IBaseModule.CreateImage360DegreeResponse> call, @NonNull Throwable t) {
                callback.failure(t);
            }
        });
    }
    
 ---------------------------------------------------------------------------   
 public class HttpGetRequest extends AsyncTask<String, Void, String> {
    private static final String REQUEST_METHOD = "GET";
    private static final int READ_TIMEOUT = 15000;
    private static final int CONNECTION_TIMEOUT = 15000;

    @Override
    protected String doInBackground(String... params) {
        String stringUrl = params[0];
        String result;
        String inputLine;
        try {
            //Create a URL object holding our url
            URL myUrl = new URL(stringUrl);
            //Create a connection
            HttpURLConnection connection = (HttpURLConnection)
                    myUrl.openConnection();
            //Set methods and timeouts
            connection.setRequestMethod(REQUEST_METHOD);
            connection.setReadTimeout(READ_TIMEOUT);
            connection.setConnectTimeout(CONNECTION_TIMEOUT);

            //Connect to our url
            connection.connect();
            //Create a new InputStreamReader
            InputStreamReader streamReader = new
                    InputStreamReader(connection.getInputStream());
            //Create a new buffered reader and String Builder
            BufferedReader reader = new BufferedReader(streamReader);
            StringBuilder stringBuilder = new StringBuilder();
            //Check if the line we are reading is not null
            while ((inputLine = reader.readLine()) != null) {
                stringBuilder.append(inputLine);
            }
            //Close our InputStream and Buffered reader
            reader.close();
            streamReader.close();
            //Set our result equal to our stringBuilder
            result = stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            result = null;
        }
        result = prepareResult(result);
        return result;
    }
    protected void onPostExecute(String result){
        super.onPostExecute(result);
    }
    private String prepareResult(String s){
        s = s.replace("[","");
        s = s.replace("]","");
        String [] strings = s.split(",");
        String result = null;
        for(int i=0; i<strings.length; i++){
            strings[i] = strings[i].replace(" ","");
            strings[i] = strings[i].replace("\"","");
            if(strings[i].contains("https")){
                result = strings[i];
            }
        }
        return result;
    }
}
// executing this asynktask

myUrl = "https://ru.wikipedia.org/w/api.php?action=opensearch&search="+word+"&limit=1&namespace=0&format=json";
                    }
                    String result = null;
                    //Instantiate new instance of our class
                    HttpGetRequest getRequest = new HttpGetRequest();
                    //Perform the doInBackground method, passing in our url
                    try {
                        result = getRequest.execute(myUrl).get();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }

    

Comments (0)