Java NIO。 SocketChannel.read方法一直返回0.为什么?

我试着了解java NIO是如何工作的。 特别是SocketChannel如何工作。 我写下面的代码: import java.io.*; import java.net.*; import java.nio.*; import java.nio.channels.*; public class Test { public static void main(String[] args) throws IOException { SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress(“google.com”, 80)); while (!socketChannel.finishConnect()) { // wait, or do something else… } String newData = “Some String…”; ByteBuffer buf = ByteBuffer.allocate(48); buf.clear(); buf.put(newData.getBytes()); buf.flip(); while […]

模拟android.content.res.Configuration类型的对象并为其指定一个语言环境

我有一个class级,我试图检索设备的国家: context.getResources().getConfiguration().locale.getCountry(); context的类型为: android.content.Context 所以在这里, context.getResources()返回一个android.content.res.Resources类型的对象。 在该对象上,调用getConfiguration() ,返回android.content.res.Configuration类型的对象。 在那,我正在访问字段locale ,它是java.util.Locale类型。 在unit testing中,我试图模拟整个上下文: Locale locale = new Locale(DEFAULT_LANGUAGE, DEFAULT_COUNTRY); configuration = new Configuration(); configuration.setLocale(locale); 但是,我收到错误,因为setLocale实现为: public void setLocale(Locale loc) { throw new RuntimeException(“Stub!”); } 或者,我尝试使用Mockito模拟整个Configuration类: mock(Configuration.class); 但是,我无法做到这一点,因为该类被宣布为final 。 那么,我如何模拟android.content.res.Configuration类型的对象并给它一个语言环境?

Java Reflection私有方法与参数最佳方法?

我试图使用javareflection调用私有方法我开发了一个小方法来调用其他方法迭代所有方法并按名称和参数类型进行比较我已成功调用4个方法。 我有一个问题。 1)。 这是最好的方法吗? 因为我理解class.getMethod只匹配公共方法。 Java内置了什么? 这是我的代码。 public class Compute { private Method getMethod(Class clazz,String methodName,Class…parametersClass) { outer: Method[] methods = clazz.getDeclaredMethods(); for(Method method:methods) { //comparing the method types… of the requested method and the real method….. if(method.getName().equals(methodName) && parametersClass.length== method.getParameterTypes().length)//it a possible match { Class[]arrayForRealParameters = method.getParameterTypes(); //comparing the method types… of the requested […]

InvocationTargetException使用appengine-gcs-client-0.5 dev_appserver存储文件

我正在使用appengine-gcs-client-0.5并在调用GcsService.createOrReplace和GcsOutputChannel.close时在dev_appserver中看到InvocationTargetExceptions。 似乎对storeBlob的调用没有适当的权限,因为appserver在com.google.appengine.api.blobstore.dev.FileBlobStorage.storeBlob中获取了AccessControlException: java.security.AccessControlException: access denied (“java.io.FilePermission” “/tmp/1440435923000-0/encoded_gs_key:” “write”) 为了让这个工作,我需要做什么?

仅当没有Exception的映射时,才会调用@ExceptionHandler for Error

使用spring-web-4.2.6,我有以下Controller和ExceptionHandler: @ControllerAdvice public class ExceptionsHandler { @ExceptionHandler(Exception.class) public ResponseEntity HandleDefaultException(Exception ex) { … } @ExceptionHandler(InternalError.class) public ResponseEntity HandleInternalError(InternalError ex) { … } } @RestController @RequestMapping(“/myController”) public class MyController { @RequestMapping(value = “/myAction”, method = RequestMethod.POST) public boolean myAction() { throw new InternalError(“”); } } 出于某种原因,调用ExceptionsHandler的HandleDefaultException(对于Exception.class)方法,但类型为NestedServletException,而不是HandleInternalError调用。 删除默认调用时,将使用正确的InternalErrorexception调用IntenalError调用。 我不想删除默认调用,因为对我来说,拥有一个默认处理程序以便为我的用户提供更好的体验非常重要。 我在这里想念的是什么? 编辑: 显然我正在使用spring-web-4.3.3,而不是要求它。 我不明白为什么,这是我的Gradle依赖树: http : //pastebin.com/h6KXSyp2

Log4j2在指定的持续时间后自动翻转

我正在使用RollingFile appender。 我希望每20分钟后滚动一次日志文件,而不管日志记录事件如何。 例如,在一小时内我应该有3个日志文件,即使那个小时可能没有任何记录。 这可能使用Log4j2吗? 如果是,请提供所需的配置(在log4j2.xml中)。 以下配置似乎不起作用: %m%n

Java中接口实现的inheritance

我有两个关于Java接口的问题。 1)如果一个类碰巧实现了接口I的所有接口方法,而没有声明自己实现它们,它仍然可以用作I类变量的输入吗? 2)实现接口I的A类的子类是否inheritance了该接口的一致性,还是应该声明自己实现I?

如何在AndEngine中确定SVG精灵的宽度和高度

我正在使用AndEngine加载svg图像作为我的精灵。 我遇到的问题是我无法弄清楚如何缩放图像以适应运行它的特定设备。 sprite= new Sprite(positionX, positionY, WIDTH,HEIGHT,TextureRegion); 它所采用的参数是x和y坐标上的位置,然后是宽度和高度。 我遇到的问题是我无法弄清楚如何根据我的需要将精灵缩放到正确的宽度和高度。 例如,我有一个标题,我希望标题在更大的设备上更大,我如何决定设备是否更大并将其扩展为更大的屏幕而缩小为更小的屏幕?

Android应用上的Google电子钱包集成

我正在开发一个Android应用程序,我必须在按钮点击上实现自动付款,如Uber Cab app必须支付出租车费用。 Uber Cab使用Google钱包作为支付网关非常好。 我想使用谷歌钱包因为使用贝宝客户应该每次都插入卡片详细信息。 但不是谷歌钱包的情况。 我没有获得在我的应用程序上实施Google钱包的良好资源。 我需要你的帮助。

在android数据库中存储浮点数

我试图在android提供的sqlite数据库中存储美元金额,但它似乎无法正常工作。 仅当我尝试删除不是整数美元金额的交易时才会出现此错误。 即409.00将起作用,但410.44将不起作用。 如果我存储一个像410.44这样的值,然后在数据库中查询该数量,我就得不到匹配。 我试图将此字段用作REAL数据类型和TEXT数据类型,但都不起作用。 在以下示例中,getTransAmount()方法返回一个float。 这是我将db数据类型设置为REAL时运行的版本。 任何帮助,将不胜感激。 谢谢。 cv.put(transAmount, t.getTransAmount()); db.insert(tableName, null, cv); x = db.delete(tableName, transAmount + ” = ” + t.getTransAmount(), null);