小编Bev*_*vor的帖子

HashMap.containsKey() - 如何搜索类?

您好,
如果您搜索HashMap<String,String>键值对的特定值,则可以编写以下内容:

myHashMap.containsKey(myString);
Run Code Online (Sandbox Code Playgroud)

但是,如果密钥不是字符串,我该如何管理呢?我有一个看起来像这样的课:

public class Kategorie implements Comparable {
    private String name;

    public Kategorie()  {
        super();
    }

    public Kategorie(String name)  {
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Object o) {
        if (!(o instanceof Kategorie))  
           throw new ClassCastException();

        Kategorie k = (Kategorie)o;
        String name = k.getName();
        return this.getName().compareTo(name);

    }
}
Run Code Online (Sandbox Code Playgroud)

在地图中,我保存了此类型"Kategorie"的键和值.

mapKategorieDEundEN.put(new Kategorie(strName_de), new Kategorie(strName_en));
Run Code Online (Sandbox Code Playgroud)

稍后在代码中,我想检查是否存在具有特定字符串的键.

if (mapKategorieDEundEN.containsKey(searchString))  {
Run Code Online (Sandbox Code Playgroud)

...不起作用,因为键不是字符串而是"Kategorie",这很清楚.

然后我尝试了这样的事情:

if (mapKategorieDEundEN.containsKey(new …
Run Code Online (Sandbox Code Playgroud)

java search hashmap

5
推荐指数
1
解决办法
1814
查看次数

在编译的应用程序中隐藏密码/密钥

在我的C应用程序中,我有一个解密密钥,用于解密数据库中的集合(用户名/密码).目前,我只是宣布它

char * key = "$$$secretSampleDecryptionKey$$$";
Run Code Online (Sandbox Code Playgroud)

在该行之后不久,我准备SQL语句,然后从DB中进行选择.我的问题是,如果有人要调试我编译的应用程序或解组它,他们真的会看到密钥吗?我该怎么做才能将它们隐藏起来?

编辑:

正如Mark和Aaron指出的那样,我可以简单地使用Linux/Unix字符串命令

strings nameOfApplication
Run Code Online (Sandbox Code Playgroud)

打印出我的应用程序中的所有字符串,包括"秘密"密钥.

编辑2:

该应用程序在我的服务器上运行,数据库存储加密的敏感客户数据.我以为我是安全的,因为没有文本文件中的密钥供所有人阅读,而是编译它.

c cryptography

5
推荐指数
1
解决办法
4351
查看次数

Java使用synchronized时我是否免费获得易失性功能?

我读了几篇关于并发性问题的帖子,但我仍然不确定某些事情.我可以说当使用synchronized时,我免费获得易失性功能,因为当释放对象的锁定时,下一个线程总是读取修改后的对象.对于volatile,对象的值会立即反映到其他线程.但是当我使用synchronized时,由于对象的锁定,不可能立即反映它.释放锁定后,只有另一个线程可以访问它.所以我不必关心立即将值反映到其他线程.我理解这个吗?

[更新]
示例打印总是1 2 3 4 5 6 7 8 9没有易失性.

package main;

public class Counter
{
    public static long count = 0;
}

public class UseCounter implements Runnable
{
    public void increment()
    {
        synchronized (this)
        {       
            Counter.count++;
            System.out.print(Counter.count + " ");
        }
    }

    @Override
    public void run()
    {
        increment();
        increment();
        increment();
    }
}

public class DataRace
{
    public static void main(String args[])
    {
        UseCounter c = new UseCounter();

        Thread t1 = new Thread(c);
        Thread t2 = new Thread(c);
        Thread …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading volatile synchronized

5
推荐指数
1
解决办法
183
查看次数

使用静态方法的类在Android中存在多长时间?

这是我之前的一个问题的后续问题.

我有一个LoadingActivity,它加载所有活动所需的一些图形并将其存储到静态类中.当我按下HOME并恢复应用程序时,我尝试不再重新加载LoadingActivity,因为它需要大量内存并且在多次运行之后已经加载了图形,因此无需再次启动LoadingActivity.我的问题是,静态课有多长时间了?我可以依靠它的可用性恢复后的应用程序,或者可以说,它是在这里(如手机运行,这意味着只要)由于Android它杀死因内存问题或者是它一直在这里只要虚拟机运行?

memory android static-members dalvik

5
推荐指数
1
解决办法
454
查看次数

ModelMapper:将集合映射到其他结构的集合

我将业务对象映射到实体,并且在某些情况下实体的结构与业务对象不同。
我将userCategories它们作为字符串存储在业务对象中RecipeBo,因为 BO 不需要了解有关实体的内部结构的任何信息。这些字符串需要映射到Recipe和的关系RecipeUserCategoryRel,除此之外,另一个字段userIdRecipeBo需要映射到其中RecipeUserCategoryRel

我的方法(有效)是创建一个包装器并手动创建关系,但这看起来像是修补:

public class BoMapper
{
    private final static ModelMapper modelMapper = new ModelMapper();

    static
    {
        modelMapper.addMappings(new IngredientMap());
    }

    public static void map(Object from, Object to)
    {
        modelMapper.map(from, to);

        if (from instanceof RecipeBo && to instanceof Recipe)
        {
            RecipeBo recipeBo = (RecipeBo)from;
            List<String> userCategories = recipeBo.getUserCategories();
            List<RecipeUserCategoryRel> recipeUserCategoryRels = new ArrayList<>();

            for (String userCategory : userCategories)
            {
                recipeUserCategoryRels.add(new RecipeUserCategoryRel(userCategory, recipeBo.getUserId()));
            }

            Recipe recipe …
Run Code Online (Sandbox Code Playgroud)

java mapping modelmapper

5
推荐指数
1
解决办法
8213
查看次数

Android:如何在 Google Play Developer Console 中获取有关捕获异常的信息?

当部署在 Google Play 商店中的应用程序崩溃时,您会看到这些崩溃报告。假设我有捕获并记录的异常,有Log.e没有办法以任何方式查看这些异常?换句话说:当我想查看对我的工作流程来说真正重要的错误时,是在 Google Play Developer Console 中查看这些异常以RuntimeException在应用程序内部抛出一个故意使应用程序崩溃的唯一方法吗?(那真的很愚蠢)。

背景:我目前正在实施应用内计费。在购买过程中,确实有很多阶段可能会发生异常。如果客户联系我并询问为什么购买过程不起作用,我怎么知道我在 Google Play Developer Console 中没有看到任何信息?

android in-app-purchase in-app-billing google-play

5
推荐指数
1
解决办法
1342
查看次数

Wildfly:ExceptionMapper不是通过RestEasy JSR-303 Bean验证触发的

我在Wildfly 8.2.0中使用Bean验证和RestEasy.最后:

@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserEndpoint
{
    //more code

    @GET
    @Path("/encrypt/{email}")
    public Response fetchEncryptedId(@PathParam("email") @NotNull String email)
    {
        String encryptedUserId = userService.getEncryptedUserId(email);

        return Response.ok().entity(new UserBo(encryptedUserId)).build();
    }
}
Run Code Online (Sandbox Code Playgroud)

这基本上有效.现在我想把响应作为JSON对象,但我不能让它工作.我的所有"应用程序"异常都由我的异常映射器处理,这有效:

@Provider
public class DefaultExceptionMapper implements ExceptionMapper<Exception>
{
    private static final String MEDIA_TYPE = "application/json";

    private LoggingService loggingService;

    @EJB
    public void setLoggingService(LoggingService loggingService)
    {
        this.loggingService = loggingService;
    }

    @Override
    public Response toResponse(Exception exception)
    {
        ResponseObject responseObject = new ResponseObject();
        responseObject.registerExceptionMessage(exception.getMessage());

        if (exception instanceof ForbiddenException)
        {
            loggingService.log(LogLevel.ERROR, ((ForbiddenException)exception).getUserId(), ExceptionToStringMapper.map(exception));
            return Response.status(Status.FORBIDDEN).type(MEDIA_TYPE).entity(responseObject).build();
        }

        //more …
Run Code Online (Sandbox Code Playgroud)

json resteasy bean-validation wildfly wildfly-8

4
推荐指数
1
解决办法
1988
查看次数

使用Google Play开发者API进行服务器端授权?

需要获得授权才能从Google Play开发者API获取信息。

我知道如何使用Postman进行此操作,但是实现授权要麻烦得多(重定向URL,处理重定向等),这些步骤就是您已经在Google Developer API Console中设置auth数据的步骤。

1.) GET https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=http://www.myurl.com/oauth2callback&client_id=1234567890.apps.googleusercontent.com
2.) get code which was sent to redirect url. 
3.) POST https://accounts.google.com/o/oauth2/token
with
    grant_type:authorization_code
    code:[the code I got before]
    client_id:1234567890.apps.googleusercontent.com
    client_secret:[my client secret]
4.) Invoke GET https://www.googleapis.com/androidpublisher/v2/applications/packageName/purchases/subscriptions/subscriptionId/tokens/token
with:
  Scope: https://www.googleapis.com/auth/androidpublisher
and:
  access_token as query parameter I got before.
Run Code Online (Sandbox Code Playgroud)

现在,我要以编程方式完成所有这些操作。显然不是那么容易。我以为Google API客户端库会有所帮助,但我看不到这些库如何为我的用例提供帮助。
例如,像GoogleAuthorizationCodeFlow之类的类在请求时希望有一个用户ID,但现在不一定要有一个用户ID,因此我想知道如何以一种干净的方式使用此API。

有没有一种干净的方法可以通过一些API来更轻松/以编程方式处理OAuth2.0,以访问Google Play开发者API?否则,我必须手动实现它。

google-api google-oauth google-oauth-java-client google-oauth2

4
推荐指数
1
解决办法
3923
查看次数

如何在布局XML中无法在TextView中创建文本?

我有不同的表行,每个表都包含一些不可点击且不可选择的信息文本.但是当我在模拟器中运行它时,文本始终是可点击的.

这意味着,当我点击任何文本块时,其颜色会变为深灰色.我不希望它改变.我希望它什么都不做.

当然,我可以将深灰色设置为文本颜色,这样用户就不会看到他点击任何内容,但这不是我想要的.

我已经尝试了不同的属性,你可以在示例中看到,但没有帮助.此外,我实际需要设置哪些不可点击的TableRow或TableRow中的TextView?

这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    >
    <ScrollView 
            android:id="@+id/scrollView"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal">
    <TableLayout 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:stretchColumns="*"
            android:clickable="false">
        <TableRow
            android:layout_width="fill_parent"
            android:background="#777777"
            android:clickable="false">
            <TextView 
                 android:id="@+id/heading"
                 android:text="This is the cool heading"             
                 android:textColor="#FFFFFF"
                 android:layout_width="fill_parent"
                 android:textSize="14sp"
                 android:paddingLeft="5sp"
                 android:paddingRight="5sp"
                 android:paddingTop="2sp"
                 android:paddingBottom="2sp"
                 android:textStyle="bold" 
                 android:clickable="false"  
                 />
        </TableRow>
         <TableRow
             android:clickable="false"
             android:linksClickable="false"
             android:focusable="false"
             android:focusableInTouchMode="false">
            <TextView
                 android:text="This is example text. It should not be clickable, but it is."
                 android:textSize="14sp"
                 android:paddingLeft="5sp"
                 android:paddingRight="5sp"
                 android:paddingTop="2sp"
                 android:paddingBottom="2sp"
                 android:scrollHorizontally="false"
                 android:inputType="textMultiLine"
                 android:linksClickable="false"
                 android:clickable="false"
                 android:focusable="false"
                 android:focusableInTouchMode="false"
                 android:layout_width="0sp"
                 />
               </TableRow>
              </TableLayout> …
Run Code Online (Sandbox Code Playgroud)

android textview tablerow android-layout

3
推荐指数
1
解决办法
1万
查看次数

MapStruct:如何将输入对象传递给表达式?

在 MapStruct 版本 1.1.0.Final 中,这是可能的....

@Mappings({
    @Mapping(target = "transaction.process.details", expression = "java(MappingHelper.mapDetails(request))"),
     //more mappings
})
Response requestToResponse(Request request);
Run Code Online (Sandbox Code Playgroud)

这是可能的,因为该mapDetails方法是(巧合?)生成到requestToResponse方法中的。这就是为什么request不为空的原因。

现在,由于 1.1.0.Final 不适用于 Lombok,我不得不升级到 1.2.0.CR2。在这个版本中,mapDetails将生成到一个单独的方法中,request没有传递,所以request现在在这个方法中为空,我得到了一个带有表达式的 NPE。(这是现在的子子方法requestToResponse。)

我是否误用了这个表达式,所以它只是巧合,还是新版本有错误?如果没有错误,我该如何request正确地将实例传递给表达式?

mapstruct

3
推荐指数
1
解决办法
1万
查看次数