I have my Android Java code that have to pass some array and some multidimensional array to the PHP Server. Unfortunately, I don't have the access to the PHP Server.
我有我的Android Java代码,必须将一些数组和一些多维数组传递给PHP Server。不幸的是,我没有访问PHP服务器的权限。
Here's the PHP Server API :
这是PHP Server API:
$result= array(
"username" => "admin",
"password" => "admin",
"hobbies" => array(
"1" => "science"
),
"skills" => array(
"1" => array(
"Boxing" => "Yes"
)
)
);
Here's my Android Java Code :
这是我的Android Java代码:
final String hobbies[] = {"science"};
final String skills[][] = {{"Boxing"},{"Yes"}};
@Override
protected Map<String, String> getParams() {
Map<String, String> jParams= new HashMap<String, String>();
JSONArray arrHobbies = new JSONArray();
JSONArray arrSkills = new JSONArray();
arrHobbies.put(hobbies[0]);
arrSkills.put(skills[0][0]);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies",arrHobbies.toString();
jParams.put("skills",arrSkills.toString();
return jParams;
}};
Then, I use this code to see how the data've been sent.
然后,我使用此代码来查看数据是如何发送的。
Log.d("Response : ", jParams.toString());
It shows this on the Android Log :
它在Android日志中显示:
{password=admin, username=admin, hobbies=["science"], skills=[["275]]}
UPDATE Here's the hash values when I debug the code:
更新这是调试代码时的哈希值:
0 = {java.util.HashMap$HashMapEntry@4884} "hobbies" -> "{"1":"1"}"
1 = {java.util.HashMap$HashMapEntry@4885} "password" -> "admin"
2 = {java.util.HashMap$HashMapEntry@4886} "username" -> "admin"
3 = {java.util.HashMap$HashMapEntry@4887} "skills" -> "{"1":{"Boxing":"Yes"}}"
I never succeed to communicate with the PHP Server API. Please help me where am I missing or wrong.
我从未成功与PHP Server API进行通信。请帮助我,我在哪里错过或错了。
3 个解决方案
#1
1
there is a library how call GSON, search it on internet it's the fastest and simplest way to resolve String to Json and Json to String.
有一个库如何调用GSON,在互联网上搜索它是解析String到Json和Json到String的最快和最简单的方法。
#2
1
I think your problem is that associative array in php represents JSON object in your json string so you probably should send something like this to your PHP Server API:
我认为你的问题是php中的关联数组代表你的json字符串中的JSON对象,所以你可能应该向你的PHP Server API发送这样的东西:
{
"password": "admin",
"username": "admin",
"hobbies":{
"1": "science"
},
"skills": {
"1": {
"Boxing": "Yes"
}
}
}
Here i can't be 100% sure and it depends on your server implementation, but to achieve the json output from above, your getParam() should look like this:
这里我不能100%肯定,这取决于你的服务器实现,但要实现上面的json输出,你的getParam()应如下所示:
protected String getParams() {
JSONObject jParams = new JSONObject();
try {
JSONObject arrHobbies = new JSONObject();
arrHobbies.put("1", "science");
JSONObject boxing = new JSONObject();
boxing.put("Boxing", "Yes");
JSONObject arrSkills = new JSONObject();
arrSkills.put("1", boxing);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies", arrHobbies);
jParams.put("skills", arrSkills);
} catch (JSONException e) {
e.printStackTrace();
}
return jParams.toString();
}
#3
0
Hope this may help you :
希望这可以帮助你:
@Override protected Map getParams() {
@Override protected Map getParams(){
Map<String, String> jParams = new HashMap<String, String>();
JSONArray arrHobbies = new JSONArray();
JSONArray arrSkills1 = new JSONArray();
JSONArray arrSkills2 = new JSONArray();
arrHobbies.put("science");
arrSkills1.put("Boxing");
arrSkills1.put("Yes");
arrSkills2.put(arrSkills1);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies", arrHobbies.toString());
jParams.put("skills", arrSkills2.toString());
return jParams;
}
#1
1
there is a library how call GSON, search it on internet it's the fastest and simplest way to resolve String to Json and Json to String.
有一个库如何调用GSON,在互联网上搜索它是解析String到Json和Json到String的最快和最简单的方法。
#2
1
I think your problem is that associative array in php represents JSON object in your json string so you probably should send something like this to your PHP Server API:
我认为你的问题是php中的关联数组代表你的json字符串中的JSON对象,所以你可能应该向你的PHP Server API发送这样的东西:
{
"password": "admin",
"username": "admin",
"hobbies":{
"1": "science"
},
"skills": {
"1": {
"Boxing": "Yes"
}
}
}
Here i can't be 100% sure and it depends on your server implementation, but to achieve the json output from above, your getParam() should look like this:
这里我不能100%肯定,这取决于你的服务器实现,但要实现上面的json输出,你的getParam()应如下所示:
protected String getParams() {
JSONObject jParams = new JSONObject();
try {
JSONObject arrHobbies = new JSONObject();
arrHobbies.put("1", "science");
JSONObject boxing = new JSONObject();
boxing.put("Boxing", "Yes");
JSONObject arrSkills = new JSONObject();
arrSkills.put("1", boxing);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies", arrHobbies);
jParams.put("skills", arrSkills);
} catch (JSONException e) {
e.printStackTrace();
}
return jParams.toString();
}
#3
0
Hope this may help you :
希望这可以帮助你:
@Override protected Map getParams() {
@Override protected Map getParams(){
Map<String, String> jParams = new HashMap<String, String>();
JSONArray arrHobbies = new JSONArray();
JSONArray arrSkills1 = new JSONArray();
JSONArray arrSkills2 = new JSONArray();
arrHobbies.put("science");
arrSkills1.put("Boxing");
arrSkills1.put("Yes");
arrSkills2.put(arrSkills1);
jParams.put("username", "admin");
jParams.put("password", "admin");
jParams.put("hobbies", arrHobbies.toString());
jParams.put("skills", arrSkills2.toString());
return jParams;
}