[英]Error posting JSONObject using Volley android studio
I am sending a JSONObject by using Volley but I get : value <br of type java.lang.string cannot be converted to JSONObject我正在使用 Volley 发送一个 JSONObject,但我得到:java.lang.string 类型的值不能转换为 JSONObject
anyone knows where is the problem?!有谁知道问题出在哪里?!
here is my java code :这是我的 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);
}
});
and here is my php code :这是我的 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;
?>
You are passing params as a JSONObject.. You should override getParams method and pass ur params..您将 params 作为 JSONObject 传递。您应该覆盖getParams方法并传递您的 params ..
For your reference check the blog : Android Volley Tutorial – Making HTTP GET, POST, PUT参考博客: Android Volley Tutorial – Making HTTP GET, POST, PUT
This is parsing error.这是解析错误。 Make sure your server is returning something like this
确保您的服务器返回这样的内容
{ "websiteInfo":"this is a website for health" } { "websiteInfo":"这是一个健康网站" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.