英文:
Error posting JSONObject using Volley android studio
问题
我使用Volley发送了一个JSONObject,但是我收到以下错误消息:
"value <br of type java.lang.string cannot be converted to JSONObject"
有人知道问题出在哪里吗?
以下是我的Java代码:
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("siteName", "example.com");
jsonObject.put("field", "android");
} catch (JSONException e) {
e.printStackTrace();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
textView.setText(response.getString("websiteInfo"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(JsonActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
Volley.newRequestQueue(JsonActivity.this).add(objectRequest);
}
});
以下是我的PHP代码:
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
$website = $obj->{'siteName'};
$field = $obj->{'field'};
$output = '';
$output->websiteInfo = $website.$field;
$jsonObject = json_encode($output);
echo $jsonObject;
?>
英文:
I am sending a JSONObject by using Volley but I get :
value <br of type java.lang.string cannot be converted to JSONObject
anyone knows where is the problem?!
here is my java code :
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("siteName", "example.com");
jsonObject.put("field", "android");
} catch (JSONException e) {
e.printStackTrace();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
textView.setText(response.getString("websiteInfo"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(JsonActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
Volley.newRequestQueue(JsonActivity.this).add(objectRequest);
}
});
and here is my php code :
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
$website = $obj->{'siteName'};
$field = $obj->{'field'};
$output ='';
$output->websiteInfo = $website.$field;
$jsonObject=json_encode($output);
echo $jsonObject;
?>
答案1
得分: 1
你正在将参数传递为一个 JSONObject。你应该重写 getParams 方法并传递你的参数。
供你参考,请查阅这篇博客:Android Volley 教程 – 执行 HTTP GET、POST、PUT 请求
英文:
You are passing params as a JSONObject.. You should override getParams method and pass ur params..
For your reference check the blog : Android Volley Tutorial – Making HTTP GET, POST, PUT
答案2
得分: 0
这是解析错误。请确保您的服务器返回类似于以下内容的内容:
{
"websiteInfo": "这是一个健康网站"
}
英文:
This is parsing error. Make sure your server is returning something like this
{
"websiteInfo":"this is a website for health"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论