小编Ter*_*rry的帖子

POST请求发送json数据java HttpUrlConnection

我开发了一个java代码,使用URL和HttpUrlConnection将以下cURL转换为java代码.卷曲是:

curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}'
Run Code Online (Sandbox Code Playgroud)

我编写了这段代码,但它始终给出了HTTP代码400错误的请求.我找不到遗漏的东西.

String url="http://url.com";
URL object=new URL(url);

HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");

JSONObject cred   = new JSONObject();
JSONObject auth   = new JSONObject();
JSONObject parent = new JSONObject();

cred.put("username","adm");
cred.put("password", "pwd");

auth.put("tenantName", "adm");
auth.put("passwordCredentials", cred.toString());

parent.put("auth", auth.toString());

OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(parent.toString());
wr.flush();

//display what returns the POST request

StringBuilder sb = new StringBuilder(); …
Run Code Online (Sandbox Code Playgroud)

java post json curl httpurlconnection

97
推荐指数
4
解决办法
30万
查看次数

Android:来自xml资源的整数

我如何修改我的XML资源,或者我必须创建什么XML文件,以访问整数值的方式与访问字符串值的方式相同R.string.some_string_resource

例如,在我想说的代码中:

ProgressDialog progressBar = new ProgressDialog(getContext());
progressBar.setMax(getInteger(R.integer.maximum));
Run Code Online (Sandbox Code Playgroud)

可能吗?

android android-resources

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

我什么时候应该使用ACCESS_COARSE_LOCATION权限?

我正在构建一个Android应用程序,它将跟踪用户的地理位置并在地图上绘制他们的路线.

我使用的是谷歌播放服务的位置API,如所描述这里.

我的应用程序需要获得ACCESS_FINE_LOCATION许可才是直观的,我将其放入清单中:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)

我还需要获得ACCESS_COARSE_LOCATION许可吗?我需要粗略位置的用例是什么?

android android-location android-permissions google-play-services google-geolocation

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

D3:什么是Bisector?

我正在研究用D3制作图表,偶然发现了d3.bisector.但是,我不明白它是什么或从文档中做了什么.

我在Web上找到的几乎所有示例都使用Date数组,类似于官方文档中的示例:

var data = [
  {date: new Date(2011,  1, 1), value: 0.5},
  {date: new Date(2011,  2, 1), value: 0.6},
  {date: new Date(2011,  3, 1), value: 0.7},
  {date: new Date(2011,  4, 1), value: 0.8}
];

var bisect = d3.bisector(function(d) { return d.date; }).right;
Run Code Online (Sandbox Code Playgroud)

那么,除了从数组元素中选择日期对象之外,平分线器还能做什么呢?什么的*.right回报呢?

如果我有一个简单的1维数组,它有用var data = [3, 6, 2, 7, 5, 4, 8]吗?

谢谢你的启发.

javascript arrays d3.js

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

Android: when/why should I use FrameLayout instead of Fragment?

I am building a layout for large screens, that is supposed to consist of 2 different parts, a left one and a right one. For doing that I thought using 2 Fragments is the right choice.

Then I had a look on the example of the navigation with the Master/Detail-Flow. It has a 2-pane layout, where on the right is the navigation, and on the left is the detail view.

但是在那个例子中,与我期望看到的不同,对于细节视图,有一个FrameLayout然后持有a Fragment,而不是Fragment直接.

布局XML看起来像这样(一个例子):

<LinearLayout …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-framelayout

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

Android:默认AppTheme背景颜色

Android的AppTheme风格的活动的默认背景颜色是什么?

我正在寻找十六进制代码,或者我可以参考的东西.它应该是一个用于LIGHT主题,一个用于DARK主题.

或者我在哪里可以查找它们?我对所有文件感到困惑,无法找到实际说出颜色的地方.

谢谢你的帮助.

更新:

我在SDK中找到了条目,/data/values/colors.xml引用了

@android:color/background_holo_light
@android:color/background_holo_dark
Run Code Online (Sandbox Code Playgroud)

但我不能把它们作为我的观点的背景颜色:它给出一个错误,说价值不公开.有解决方法吗?

android android-theme android-styles

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

Travis CI Android测试:没有连接设备

我正在尝试为Android设置Travis.到目前为止,运行构建似乎工作,但是当涉及到测试时,它抱怨"没有连接的设备!"

:app:connectedAndroidTestDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:connectedAndroidTestDebug'.
> com.android.builder.testing.api.DeviceException: java.lang.RuntimeException: 
    No connected devices!
Run Code Online (Sandbox Code Playgroud)

这是我的.travis.yml,根据我的理解,我正在为测试创建和启动模拟器,就像文档所说的那样.

language: android
android:
  components:
    # Uncomment the lines below if you want to
    # use the latest revision of Android SDK Tools
    # - platform-tools
    # - tools

    # The BuildTools version used by your project
    - build-tools-22.0.1

    # The SDK version used to compile your project
    - android-22

    # Additional components
    - …
Run Code Online (Sandbox Code Playgroud)

continuous-integration android travis-ci

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

Travis CI构建不适用于Android Constraint Layout

我想让Travis构建我的Android项目.它尝试下载库时失败ConstraintLayout.你知道我要做些什么才能让它发挥作用吗?

我的.travis.yml是这样的:

language: android
jdk:
  - oraclejdk8
android:
  components:
    - platform-tools
    - tools
    - build-tools-23.0.2
    - android-23
    - extra-android-support
    - extra-android-m2repository
    - extra-google-m2repository
Run Code Online (Sandbox Code Playgroud)

build.gradle是:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "my.example.bdd"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha2'
    testCompile 'junit:junit:4.12'
    androidTestCompile …
Run Code Online (Sandbox Code Playgroud)

android travis-ci android-constraintlayout

22
推荐指数
3
解决办法
7797
查看次数

适用于Android的最小工作SpotBugs设置

如何为Android设置SpotBugs?

我尝试遵循官方文档gradle插件的文档,但Android的设置不完整且令人困惑,并且无法正常工作.

我尝试了以下设置.

build.gradle(项目):

buildscript {
  repositories {
    // ...
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    // ...
    classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

build.gradle(app):

//...
apply plugin: "com.github.spotbugs"

android {
  // ...
  sourceSets {
    main {
      java.srcDirs = ['src/main/java']
    }
  }
}

// ...

spotbugs {
    toolVersion = "3.1.3"
    ignoreFailures = true
    reportsDir = file("$project.buildDir/findbugsReports")
    effort = "max"
    reportLevel = "high"
}

tasks.withType(com.github.spotbugs.SpotBugsTask) {
  // What do I need to do here?
}
Run Code Online (Sandbox Code Playgroud)

我试过运行它 …

android gradle android-studio spotbugs

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

Android Studio:为if语句抑制lint警告

我的Android项目中有一个代码:

public boolean isLoadInProgress(boolean privateLoad, boolean publicLoad) {
    if (privateLoad && privateLoadInProgress) {
        return true;
    }
    if (publicLoad && publicLoadInProgress) {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我在第二个if语句中得到一个lint警告:'if'语句可以简化.这显然是因为我也可以写:

return publicLoad && publicLoadInProgress;
Run Code Online (Sandbox Code Playgroud)

但是,为了便于阅读,我想保持这种方式.我知道有一些内联注释注释用于关闭该位置的lint警告,但我在Android Lint文档中找不到它.你能告诉我这个注释/评论是什么吗?

android lint suppress-warnings android-studio

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