JsonObjectRequest - 无法发送参数
JsonObjectRequest - cannot send arguments
我不确定为什么我无法从我的 PHP 代码中得到正确的响应。对于我的 PHP 代码,它真的很简单。它应该只是 return POST 个参数。但是在我的 Android 项目中,它 return 对所有参数都是空的。我试图在我的 iOS 项目中测试 PHP 代码。一切都很完美。它 returns ["target": zh, "arrayString": <__NSSingleObjectArrayI 0x6000000192a0>(hello world), "source": en]
所以,我想也许我的 Android 项目有一些问题,然后我尝试创建一个新项目并再次执行,但仍然遇到同样的问题。以下是我的新 Android 项目的步骤。
建造Gradle
dependencies {
compile 'com.android.volley:volley:1.1.0'
}
Android清单
<uses-permission android:name="android.permission.INTERNET" />
主要活动
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String requestString = "http://ddoommaaiinn.com/testPost.php";
JSONObject parameters = new JSONObject();
try {
parameters.put("source", "en");
parameters.put("target", "zh");
JSONArray jsonArray = new JSONArray();
jsonArray.put("hello world");
parameters.put("stringArray", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("JSONParams", parameters.toString());
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, requestString, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//TODO: handle success
Log.d("successful", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//TODO: handle failure
}
});
Volley.newRequestQueue(this).add(jsonRequest);
}
}
调试
12-27 08:47:19.994 4363-4363/com.pakhocheung.testjsonobjectrequest D/JSONParams: {"source":"en","target":"zh","stringArray":["hello world"]}
12-27 08:47:20.050 4363-4395/com.pakhocheung.testjsonobjectrequest D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-27 08:47:20.079 4363-4397/com.pakhocheung.testjsonobjectrequest D/OpenGLRenderer: HWUI GL Pipeline
12-27 08:47:20.885 4363-4397/com.pakhocheung.testjsonobjectrequest I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest I/OpenGLRenderer: Initialized EGL, version 1.4
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest D/OpenGLRenderer: Swap behavior 1
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest D/OpenGLRenderer: Swap behavior 0
12-27 08:47:20.892 4363-4397/com.pakhocheung.testjsonobjectrequest D/EGL_emulation: eglCreateContext: 0xa6f859e0: maj 2 min 0 rcv 2
12-27 08:47:20.895 4363-4397/com.pakhocheung.testjsonobjectrequest D/EGL_emulation: eglMakeCurrent: 0xa6f859e0: ver 2 0 (tinfo 0xb050d880)
12-27 08:47:20.998 4363-4397/com.pakhocheung.testjsonobjectrequest D/EGL_emulation: eglMakeCurrent: 0xa6f859e0: ver 2 0 (tinfo 0xb050d880)
12-27 08:47:21.279 4363-4363/com.pakhocheung.testjsonobjectrequest D/successful: {"source":null,"target":null,"arrayString":null}
PHP代码
<?php
$source = $_POST['source'];
$target = $_POST['target'];
$arrayString = $_POST['stringArray'];
echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
?>
看起来你的 $_POST 没有工作
为了测试尝试将硬编码值
$source = 'test';
$target = 'test-target';
$arrayString = 'test-arrayString';
echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
它应该给出以下 json
{"source":"test","target":"test-target","arrayString":"test-arrayString"}
如有不足请补充
当使用 JsonObjectRequest
时,所需数据将作为 (json) 字符串在 php://input
中找到。要获取该数据,请执行以下操作:
<?php
$postArray = json_decode(file_get_contents('php://input'), true);
// now you access the values nearly as before:
$source = $postArray['source'];
$target = $postArray['target'];
$arrayString = $postArray['arrayString'];
echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
?>
我不确定为什么我无法从我的 PHP 代码中得到正确的响应。对于我的 PHP 代码,它真的很简单。它应该只是 return POST 个参数。但是在我的 Android 项目中,它 return 对所有参数都是空的。我试图在我的 iOS 项目中测试 PHP 代码。一切都很完美。它 returns ["target": zh, "arrayString": <__NSSingleObjectArrayI 0x6000000192a0>(hello world), "source": en]
所以,我想也许我的 Android 项目有一些问题,然后我尝试创建一个新项目并再次执行,但仍然遇到同样的问题。以下是我的新 Android 项目的步骤。
建造Gradle
dependencies {
compile 'com.android.volley:volley:1.1.0'
}
Android清单
<uses-permission android:name="android.permission.INTERNET" />
主要活动
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String requestString = "http://ddoommaaiinn.com/testPost.php";
JSONObject parameters = new JSONObject();
try {
parameters.put("source", "en");
parameters.put("target", "zh");
JSONArray jsonArray = new JSONArray();
jsonArray.put("hello world");
parameters.put("stringArray", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("JSONParams", parameters.toString());
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, requestString, parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//TODO: handle success
Log.d("successful", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//TODO: handle failure
}
});
Volley.newRequestQueue(this).add(jsonRequest);
}
}
调试
12-27 08:47:19.994 4363-4363/com.pakhocheung.testjsonobjectrequest D/JSONParams: {"source":"en","target":"zh","stringArray":["hello world"]}
12-27 08:47:20.050 4363-4395/com.pakhocheung.testjsonobjectrequest D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-27 08:47:20.079 4363-4397/com.pakhocheung.testjsonobjectrequest D/OpenGLRenderer: HWUI GL Pipeline
12-27 08:47:20.885 4363-4397/com.pakhocheung.testjsonobjectrequest I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest I/OpenGLRenderer: Initialized EGL, version 1.4
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest D/OpenGLRenderer: Swap behavior 1
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-27 08:47:20.886 4363-4397/com.pakhocheung.testjsonobjectrequest D/OpenGLRenderer: Swap behavior 0
12-27 08:47:20.892 4363-4397/com.pakhocheung.testjsonobjectrequest D/EGL_emulation: eglCreateContext: 0xa6f859e0: maj 2 min 0 rcv 2
12-27 08:47:20.895 4363-4397/com.pakhocheung.testjsonobjectrequest D/EGL_emulation: eglMakeCurrent: 0xa6f859e0: ver 2 0 (tinfo 0xb050d880)
12-27 08:47:20.998 4363-4397/com.pakhocheung.testjsonobjectrequest D/EGL_emulation: eglMakeCurrent: 0xa6f859e0: ver 2 0 (tinfo 0xb050d880)
12-27 08:47:21.279 4363-4363/com.pakhocheung.testjsonobjectrequest D/successful: {"source":null,"target":null,"arrayString":null}
PHP代码
<?php
$source = $_POST['source'];
$target = $_POST['target'];
$arrayString = $_POST['stringArray'];
echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
?>
看起来你的 $_POST 没有工作
为了测试尝试将硬编码值
$source = 'test';
$target = 'test-target';
$arrayString = 'test-arrayString';
echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
它应该给出以下 json
{"source":"test","target":"test-target","arrayString":"test-arrayString"}
如有不足请补充
当使用 JsonObjectRequest
时,所需数据将作为 (json) 字符串在 php://input
中找到。要获取该数据,请执行以下操作:
<?php
$postArray = json_decode(file_get_contents('php://input'), true);
// now you access the values nearly as before:
$source = $postArray['source'];
$target = $postArray['target'];
$arrayString = $postArray['arrayString'];
echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString));
?>