你可以参考我下面的示例代码:
更新您的引擎收录链接:
由于服务器响应的JSONArray
,我用JsonArrayRequest
代替JsonObjectRequest
。并且不需要覆盖getBody
了。
mTextView = (TextView) findViewById(R.id.textView);
String url = "https://api.orange.com/datavenue/v1/datasources/2595aa553d3049f0b0f03fbaeaa7ddc7/streams/9fe5edb1c76e4968bdcc9c902010bc6c/values";
RequestQueue requestQueue = Volley.newRequestQueue(this);
final String jsonString = "[\n" +
" {\n" +
" \"value\": 1\n" +
" }\n" +
"]";
try {
JSONArray jsonArray = new JSONArray(jsonString);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url, jsonArray, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
mTextView.setText(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText(error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("X-OAPI-Key","TQEEGSk8OgWlhteL8S8siKao2q6LIGdq");
headers.put("X-ISS-Key","2b2dd0d9dbb54ef79b7ee978532bc823");
return headers;
}
};
requestQueue.add(jsonArrayRequest);
} catch (JSONException e) {
e.printStackTrace();
}
我的代码同时适用于谷歌的官方凌空libray和mcxiaoke图书馆
如果你想使用谷歌的图书馆,你的Git克隆作为谷歌文档后,从\src\main\java\com
复制的Android文件夹到你的项目为下面的截图中\app\src\main\java\com
(你克隆排球项目):
的build.gradle
应包含以下
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.code.gson:gson:2.3.1'
}
如果你的项目使用mcxiaoke的图书馆,在build.gradle
将类似于以下(注意dependencies
):
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.example.samplevolley"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
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.0.0'
compile 'com.mcxiaoke.volley:library:1.0.17'
compile 'com.google.code.gson:gson:2.3'
}
我建议您将创建2个新的示例项目,然后将使用Google的图书馆,另一个将使用mcxiaoke的图书馆。 UPDATE OF
END
String url = "http://...";
RequestQueue requestQueue = Volley.newRequestQueue(this);
final String jsonString = "[\n" +
" {\n" +
" \"value\": 1\n" +
" }\n" +
"]";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// do something...
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// do something...
}
}) {
@Override
public byte[] getBody() {
try {
return jsonString.getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
jsonString, PROTOCOL_CHARSET);
return null;
}
}
};
requestQueue.add(jsonObjectRequest);
下面的截图是领受的服务器端Web服务:
来源
2015-09-25 06:13:50
BNK
阅读[我asnwer这里(http://stackoverflow.com /问题/ 32197615 /抽射,发送-的JSONObject到服务器与 - 法柱/ 32216762#32216762)。但是,您的json是JSONArray,而不是JSONObject。 – BNK
你可以更明白吗?我仍然无法编码这个jsonArray ...谢谢你的理解 – fujitsu4
JSONObject jsonBody = new JSONObject(“{\”value \“:1}”); JSONObject以{开始并以}结尾。而且,你得到了什么错误信息?请发布它和任何logcat信息(如果可用)。 – BNK