小编soc*_*qwe的帖子

HttpClient:确定响应中的空实体

我想知道如何确定一个空的http响应.使用空的http响应我的意思是,http响应只会设置一些标题,但包含一个空的http主体.

例如:我对网络服务器进行HTTP POST,但网络服务器只会返回我的HTTP POST的状态代码,而不会返回任何其他内容.

问题是,我在apache HttpClient上编写了一个小的http框架来进行自动json解析等.所以这个框架的默认用例是发出请求并解析响应.但是,如果响应不包含数据,如上例中所述,我将确保我的框架跳过json解析.

所以我这样做:

HttpResponse response = httpClient.execute(uriRequest);
HttpEntity entity = response.getEntity();
if (entity != null){
    InputStream in = entity.getContent();
    // json parsing
}
Run Code Online (Sandbox Code Playgroud)

但实体总是!= null.并且检索到的输入流是!= null.有没有一种简单的方法来确定http正文是否为空?

我看到的唯一方法是服务器响应包含设置为0的Content-Length头字段.但并非每个服务器都设置此字段.

有什么建议?

java android httpclient

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

资源是颜色还是可绘制的?

我创建了一个扩展ImageView的自定义视图.我的自定义视图提供了一种方法showError(int),我可以在其中传递资源ID,该资源ID应显示为图像视图内容.如果我可以传递简单的颜色资源ID或可绘制的资源ID,那将是很好的.

我的问题是:如何确定传递的资源ID是Drawable还是Color?

我目前的做法是这样的:

class MyImageView extends ImageView{

     public void showError(int resId){

        try{
            int color = getResources().getColor(resId);
            setImageDrawable(new ColorDrawable(color));
        }catch(NotFoundException e){
            // it was not a Color resource so it must be a drawable
            setImageDrawable(getResources().getDrawable(resId));
        }

     }
}
Run Code Online (Sandbox Code Playgroud)

这样做安全吗?我的假设是,资源ID真的很独特.我的意思是在R.drawable或R.color中不是唯一的,但在完全独特R

所以没有

R.drawable.foo_drawable = 1;
R.color.foo_color = 1;
Run Code Online (Sandbox Code Playgroud)

是否正确将id 1分配给其中一个资源而不是两者都分配?

android android-resources

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

如何指定maven编译器插件顺序

我正在使用maven进行混合java和kotlin项目.

我现在面临的问题是,maven-compiler-plugin在编译之前运行kotlin-maven-plugin.

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @annotation --- 
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to /Users/hannes/workspace/tikxml/annotation/target/classes 
[INFO] --- kotlin-maven-plugin:1.0.0-beta-4583:compile (compile) @ annotation 
[INFO] Kotlin Compiler version 1.0.0-beta-4583
Run Code Online (Sandbox Code Playgroud)

在我的java源代码中,我引用了用kotlin编写的类.但是javac在kotlinc之前运行.因此,maven会因编译器错误而中断.

我的pom(父pom,我使用子模块)看起来像这样:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>

    ...

    <modules>
        <module>core</module>
        <module>annotation</module>
        <module>processor</module>
    </modules>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <kotlin.version>1.0.0-beta-4583</kotlin.version>
    </properties>


    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
            </plugins>


        </pluginManagement>

        <plugins>

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

java maven kotlin

3
推荐指数
2
解决办法
1663
查看次数

PHP MVC:我真的需要一个控制器吗?

我的问题很简单,但(我猜)很难回答:

我的PHP网站/ Web应用程序中是否真的需要一个完整的模型 - 视图 - 控制器设计模式?

我无法理解Controller如何对PHP有用,因为每个PHP站点都是在每个请求上动态生成的.因此,在PHP生成站点之后,无法让View(生成的HTML站点在浏览器中)与控制器交互,因为Controller位于服务器端并为每个站点请求生成一次,即使请求是AJAX ...

我明白完全错了吗?

为什么我应该使用任何类型的MVC PHP框架,如Zend或Symphony?

编辑:例如假设,应该有一个网站来表示表中的客户列表:

我的模型将是服务器端的一个简单类客户,用于查询数据库.我的视图将是生成的HTML代码,显示模型(客户列表).和控制器?控制器是仅评估GET或POST来调用模型的正确方法吗?

php model-view-controller controller

2
推荐指数
1
解决办法
1768
查看次数

我可以将Intent Service放入库项目并在不同的应用程序中使用它吗?

我正在使用Intent Service工作.我想在不同的应用程序中使用此IntentService,这些应用程序可能并行安装在用户设备上.

我们假设我有两个Android应用程序.两者都使用我的库,并希望使用我的IntentService(库的一部分).我不想做类似"共享服务"的东西,两个应用程序都是独立的,两个服务应该彼此独立运行.

在Android Manifest的第一个应用程序中,我指定服务如下:

<service android:name="com.mylibrary.service.SuperService" android:enabled="true"/>
Run Code Online (Sandbox Code Playgroud)

在第二个应用程序中,我将做同样的事情:

<service android:name="com.mylibrary.service.SuperService" android:enabled="true"/>
Run Code Online (Sandbox Code Playgroud)

是否会产生冲突,因为这两个应用程序具有相同的包和名称?Android知道哪个SuperService实例属于哪个应用程序?

android android-intent android-service

2
推荐指数
1
解决办法
1726
查看次数

获取Java注释处理器中的数组类型

我正在写一个注释处理器.我怎样才能获得数组的类型?

@MyAnnotation
int[] iArray;


@MyAnnotation
boolean[] bArray;


@MyAnnotation
FooClass[] fooArray;
Run Code Online (Sandbox Code Playgroud)

据我所知,我可以检查它是否是这样的数组:

if (element.asType().getKind() == TypeKind.ARRAY) {
   // it's an array
   // How to check if its an array of boolean or an array integer, etc.?
}
Run Code Online (Sandbox Code Playgroud)

我如何获得数组的类型?

基本上我迭代所有注释的元素@MyAnnotation,我将根据数组的类型对数组做一些特殊的事情,类似于:

for (Element element : enviroment.getElementsAnnotatedWith(MyAnnotation.class)) {
    if (element.getKind() != ElementKind.FIELD)
        continue;

    if (element.asType().getKind() == TypeKind.ARRAY) {
        // it's an array
        // How to distinguish between array of boolean or an array integer, etc.?
    }
}
Run Code Online (Sandbox Code Playgroud)

java annotations annotation-processing

2
推荐指数
1
解决办法
416
查看次数

如何撤消git rm

我不小心添加并将一个(巨大的)日志文件提交到我的本地git存储库,然后执行了git rm(没有--cached).所以历史看起来像这样:

> git status

modified:  Foo.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    huge_file.log

nothing added to commit but untracked files present (use "git add" to track)
Run Code Online (Sandbox Code Playgroud)

然后我做了git add .:

> git add .
> git status

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    modified:  Foo.java
    new file:   huge_file.log
Run Code Online (Sandbox Code Playgroud)

然后(我忽略了我已经添加huge_file.log)我做了一个git commit:

> git commit -am "Foo commit"
[master 7582793] Foo commit …
Run Code Online (Sandbox Code Playgroud)

git

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

将热 observable 转换为冷 observable

我正在用 RxJava 观察数据库表。所有 SQL SELECT 查询都返回一个热 observable,每当我在同一个表中插入/更新/删除行时,我将重新运行任何订阅的 SELECT 查询并调用onNext(queryResult)热查询 observable。

database.select("SELECT * FROM Foo) // returns an observable
       .map(doSomething)
       .filter(aFilter)
       .subscribe (
             { 
                 //  onNext
             },
             {
                // onError
             }
          )
Run Code Online (Sandbox Code Playgroud)

这很有效。但是,在一个特殊用例中,我想避免数据库重新发出新项目。是否有某种操作符我可以用来使它成为一个单一的“冷”可观察对象,而不必更改我为热可观察对象构建的所有数据库层,如下所示:

database.select("SELECT * FROM Foo) // returns an observable
           .map(doSomething)
           .filter(aFilter)
           .toColdObservable()
           .subscribe (
                 { 
                     //  onNext
                 },
                 {
                    // onError
                 }
              )
Run Code Online (Sandbox Code Playgroud)

我知道“cold observable”这个词不合适,但我还没有找到更好的名字。所以我想要的是数据库不通知 SQL Query Observable 关于更新。所以我对“冷可观察”的意思只是查询数据库一次,之后不再接收更新。

rx-java

2
推荐指数
1
解决办法
2303
查看次数

永久定期运行的间隔

好的,通常可以Observable.interval(1, TimeUnit.SECONDS)用来创建一个每秒都会发出一个值的observable.

但是interval发出一个整数值.如果我们让它运行到ÌNT_MAX`会发生什么?溢出?

是否存在可以执行相同操作的运算符,即在没有计数器的情况下定期发送值.

基本上我只想要一个Observable安排定期工作......有点......

rx-java

2
推荐指数
1
解决办法
115
查看次数