小编Bev*_*vor的帖子

将字节数组转换为字符串并返回字节数组的问题

关于这个主题有很多问题,同样的解决方案,但这对我不起作用.我有一个加密的简单测试.加密/解密本身是有效的(只要我使用字节数组本身而不是字符串处理此测试).问题是不希望将它作为字节数组处理,而是作为String处理,但是当我将字节数组编码为字符串并返回时,生成的字节数组与原始字节数组不同,因此解密不再起作用.我在相应的字符串方法中尝试了以下参数:UTF-8,UTF8,UTF-16,UTF8.他们都没有工作.生成的字节数组与原始数组不同.任何想法为什么会这样?

加密:

public class NewEncrypter
{
    private String algorithm = "DESede";
    private Key key = null;
    private Cipher cipher = null;

    public NewEncrypter() throws NoSuchAlgorithmException, NoSuchPaddingException
    {
         key = KeyGenerator.getInstance(algorithm).generateKey();
         cipher = Cipher.getInstance(algorithm);
    }

    public byte[] encrypt(String input) throws Exception
    {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] inputBytes = input.getBytes("UTF-16");

        return cipher.doFinal(inputBytes);
    }

    public String decrypt(byte[] encryptionBytes) throws Exception
    {
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
        String recovered = new String(recoveredBytes, "UTF-16");

        return recovered;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试的测试:

public class NewEncrypterTest
{
    @Test
    public …
Run Code Online (Sandbox Code Playgroud)

java string encryption bytearray

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

如何在Android/Maven项目中处理缺少的src/test/java源文件夹?

我对Maven与Android的结合并不是很有经验,所以我按照这些说明制作了一个新的Android项目.创建项目后,我收到以下错误消息:

Project'xxx-1.0-SNAPSHOT'缺少必需的源文件夹:'src/test/java'

当我尝试使用带有src/test/java的New-> Other-> Java-Source Folder添加新的源文件夹时,我收到另一条错误消息:

该文件夹已是源文件夹.

但我的项目中没有任何src/test/java文件夹.我应该怎么处理?设置项目的干净方法是什么,因为我认为此指令中缺少某些内容.那么让src/test/java出现的Maven方法是什么?

我正在使用Eclipse Juno,m2e 1.1.0,针对m2e 0.4.2的Android配置.

eclipse android m2eclipse maven m2e

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

Java字符串值的功能发生了变化

我有这个非常尴尬的问题......

void changeString(String str){
    str = "Hello world":
}

main(){
    String myStr = new String("");
    changeString(myStr);
}
Run Code Online (Sandbox Code Playgroud)

main返回时,该值仍然"""Hello world".这是为什么?

另外,我如何使其工作?假设我希望我的函数changeString将其变为"Hello world"的字符串.

java string function pass-by-reference

24
推荐指数
2
解决办法
4万
查看次数

如何用回车创建一个String?

对于JUnit测试,我需要一个由多行组成的String.但我得到的只是一个单行字符串.我尝试了以下方法:

    String str = ";;;;;;\n" +
                 "Name, number, address;;;;;;\n" + 
                 "01.01.12-16.02.12;;;;;;\n" + 
                 ";;;;;;\n" + 
                 ";;;;;;";
Run Code Online (Sandbox Code Playgroud)

我也试过\n\r而不是\n.System.getProperty("line.separator")也不起作用.它产生一个\n字符串,没有回车.那我怎么解决呢?

java string

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

需要帮助来了解我的Android应用中的内存泄漏

我的应用运行正常,直到我在安装后的第一次启动中断初始化过程,只要初始化过程尚未完成,退出并启动应用程序几次.处理逻辑和AsyncTask可以很好地处理这个问题,所以我没有遇到任何不一致,但我的堆有问题.当我在应用程序设置中执行此令人不安的退出和启动时,它会越来越多,这将导致OutOfMemory错误.我已经通过MAT分析堆已经发现了泄漏,但我仍然有另一个泄漏,我无法隔离.
有关背景信息:我将应用程序上下文,列表和时间戳存储在静态类中,以便能够从应用程序中的任何位置访问它,而无需使用构造函数的繁琐的传递引用.无论如何,这个静态类(ApplicationContext)肯定有问题,因为它会因区域列表而导致内存泄漏.区域对象处理GeoJSON数据.这是这个类的样子:

public class ApplicationContext extends Application {
    private static Context context;
    private static String timestamp;
    private static List<Zone> zones = new ArrayList<Zone>();

    public void onCreate()  {
        super.onCreate();
        ApplicationContext.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return ApplicationContext.context;
    }

    public static List<Zone> getZones() {
        return zones;
    }

    public static void setData(String timestamp, List<Zone> zones) {
        ApplicationContext.timestamp = timestamp;
        ApplicationContext.zones = zones;
    }

    public static String getTimestamp() {
        return timestamp;
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试像这样存储区域

ApplicationContext.zones = new ArrayList(zones);

但它没有效果.我已经尝试将zones属性放入另一个静态类,因为ApplicationContext在所有其他类之前加载(由于AndroidManifest中的条目),这可能导致这种行为,但这也不是问题.

setData在我的"ProcessController"中调用两次.一旦进入doUpdateFromStorage,就进入doUpdateFromUrl(String).这个类看起来像这样:

public …
Run Code Online (Sandbox Code Playgroud)

heap android memory-leaks out-of-memory

12
推荐指数
1
解决办法
2221
查看次数

java.lang.IllegalArgumentException:已添加:Lorg/hamcrest/BaseDescription; 转换为Dalvik格式失败,错误1

首先,至少有2个帖子具有相同的问题,但这些解决方案不再起作用,至少在我的安装中不起作用.

我正在使用m2e与Eclipse和Android并尝试通过选择run as-> Android应用程序将该应用程序作为"Android应用程序"运行,但我总是收到此错误:

意外的顶级异常:java.lang.IllegalArgumentException:已添加:Lorg/hamcrest/BaseDescription;
...

[2012-09-08 19:50:41 - net.mydomain.project-TRUNK]转换为Dalvik格式失败,错误1

这是工具R14部分中描述的问题.首先,这不能修复,因为我在ADT 20.0.3中遇到了这个问题.其次,我没有这些所谓的"_src"文件夹.我以前从未在Maven项目中见过它们,所以我不知道我现在该做什么.我连两次都没有链接库.至少我在项目中没有看到一些.任何想法如何让这个工作?

这是我的pom.xml,如果这有帮助:

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.devgems.android</groupId>
    <artifactId>kurzparkzonewien</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>kurzparkzonewien</name>

    <properties>
        <platform.version>1.6_r2</platform.version>
        <android.sdk.path>/opt/android-sdk-linux</android.sdk.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- Make sure this is below the android dependencies -->
        <dependency>
            <groupId>com.pivotallabs</groupId>
            <artifactId>robolectric</artifactId>
            <version>1.0-RC1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <outputDirectory>target/classes</outputDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>

        <plugins> …
Run Code Online (Sandbox Code Playgroud)

eclipse adt maven m2e eclipse-juno

11
推荐指数
2
解决办法
5257
查看次数

Mockito:如何替换被测试类调用的类方法?

不幸的是,Mockito文档缺乏详尽的信息,很难找到如何创建以下设置的正确方法.

我有一个应该测试的类"ResourceManager".该类实例化另一个类"JerseyClient",它有一个名为"get"的方法.我希望"ResourceManager" 不要调用真正的"JerseyClient",而是从它调用mock(或stub?)(我已经不清楚Mockito上下文中模拟和存根或模拟和间谍之间的区别).

我的尝试是@Mock(或@Spy?)JerseyClient,或者至少有一种方法:

@RunWith(MockitoJUnitRunner.class)
public class ResourceManagerTest
{
    @Mock
    private JerseyClient jerseyClient;


    @Test
    public void testResultHandling() throws JsonGenerationException, JsonMappingException, IOException, ResourceException
    {
        TestEntity testEntity = new TestEntity();
        ResourceManager resourceManager = new ResourceManager();

        testEntity.setGet(true);
        testEntity.setTestAttribute("1stTest");

        when(jerseyClient.get(anyString())).thenReturn("{\"get\":true,\"testAttribute\":\"2ndTest\",\"message\":\"success\"}");

//      doReturn("{\"get\":true,\"testAttribute\":\"2ndTest\",\"message\":\"success\"}").when(jerseyClient).get(anyString());

        TestEntity result = (TestEntity)resourceManager.execute(testEntity, TestEntity.class);

        assertThat(result, is(notNullValue()));
        assertThat(result.getMessage(), is("success"));
        assertThat(result.getTestAttribute(), is("2ndTest"));
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我尝试模拟jerseyClient.get()方法以返回预定义的JSON字符串:

when(jerseyClient.get(anyString())).thenReturn("{\"get\":true,\"testAttribute\":\"2ndTest\",\"message\":\"success\"}");
Run Code Online (Sandbox Code Playgroud)

要么

doReturn("{\"get\":true,\"testAttribute\":\"2ndTest\",\"message\":\"success\"}").when(jerseyClient).get(anyString());
Run Code Online (Sandbox Code Playgroud)

但它们都不起作用.这意味着调用了真正的JerseyClient.get方法,因为它尝试使用真正的JerseyClient发出HTTP请求.

解决方案是什么,我想在这做什么?它是在监视一个真实的对象还是嘲笑一个我想要模拟它的方法的对象,什么时候可以替换方法,只能在模拟上或只在间谍上?

java junit mocking mockito

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

Android Studio - 显示Java扩展

我不确定它何时发生,但不知何故我在Android Studio 2.1.3中看不到任何资源管理器窗口中的Java文件扩展名.我想这与SVN项目共享(第一次签到)有关,因为我之前没有观察到这种行为.而不是MainActivity.java我看到MainActivity.我没有找到任何设置在哪里切换回来.有人知道这个设置在哪里吗?

intellij-idea android-studio

9
推荐指数
1
解决办法
1040
查看次数

MapStruct:如何将所有属性映射到列表的第一个元素?

我需要一个映射来实现此目的:

@Mapping(source = "a", target = "result.transactions[0].a"),
@Mapping(source = "b", target = "result.transactions[0].b"),
@Mapping(source = "c", target = "result.transactions[0].c"),
...
Response dataToResponse(DataModel model);
Run Code Online (Sandbox Code Playgroud)

但这种语法不起作用(顺便说一句:这适用于 Spring Bean 包装器)。这样的解决方案只是一个半生不熟的解决方案:

@AsList
public <T> List<T> asList( T in ) {
    List<T> result = new ArrayList<T>();
    if ( in!=null ) {
       result.add(in);
   }
   return result;
}
Run Code Online (Sandbox Code Playgroud)

这仅适用于一个属性,因为它总是为每个属性创建一个新列表。我不需要将每个属性映射到新列表的第一个元素。该列表必须重复使用,但我不知道这是如何工作的。实现这一目标的正确方法是什么?我想到了这样的事情:

@Mapping(source = "a", target = "transaction.a"),
@Mapping(source = "b", target = "transaction.b"),
@Mapping(source = "c", target = "transaction.c"),
...
Transaction dataToTransaction(DataModel model);
Run Code Online (Sandbox Code Playgroud)

进而...

@Mapping([use Transaction from …
Run Code Online (Sandbox Code Playgroud)

java mapping mapstruct

9
推荐指数
2
解决办法
2万
查看次数

JPA:TypedQuery有时会返回null而不是NoResultException

通常我使用NoResultException来返回一个"空"对象,例如一个空的错误列表或新的BigInteger("0"),如果我没有得到TypedQuery的结果.现在事实证明,这有时不起作用.突然getSingleResult()返回null而不是导致NoResultException,我不明白为什么.看这个例子:

public BigInteger pointsSumByAccountId(long accountId)
{
    try
    {
        TypedQuery<BigInteger> pointsQuery = entityManager.createNamedQuery(Points.SumByAccountId, BigInteger.class);
        pointsQuery.setParameter(Points.AccountIdParameter, accountId);

        return pointsQuery.getSingleResult();
    }
    catch (NoResultException e)
    {
        return new BigInteger("0");
    }
}
Run Code Online (Sandbox Code Playgroud)

实体的重要部分......

@NamedQueries({@NamedQuery(name = "Points.sumByAccountId", query = "select sum(p.value) from Points p where p.validFrom <= current_timestamp() and p.validThru >= current_timestamp() and p.account.id = :accountId")})
public class Points
{
    private static final long serialVersionUID = -15545239875670390L;

    public static final String SumByAccountId = Points.class.getSimpleName() + ".sumByAccountId";
    public static final String AccountIdParameter = "accountId";
.
.
. …
Run Code Online (Sandbox Code Playgroud)

jpa hql jpa-2.0

8
推荐指数
1
解决办法
6001
查看次数