错误使用Volley在Android Studio中发布JSONObject。

huangapple go评论82阅读模式
英文:

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(&quot;siteName&quot;, &quot;example.com&quot;);
        jsonObject.put(&quot;field&quot;, &quot;android&quot;);
    } 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&lt;JSONObject&gt;() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        textView.setText(response.getString(&quot;websiteInfo&quot;));
                    } 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 :

    &lt;?php 
$json = file_get_contents(&#39;php://input&#39;);

$obj = json_decode($json);

$website = $obj-&gt;{&#39;siteName&#39;};

$field = $obj-&gt;{&#39;field&#39;};

$output =&#39;&#39;;

$output-&gt;websiteInfo = $website.$field;

$jsonObject=json_encode($output);

echo $jsonObject;
?&gt;

答案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"
}

huangapple
  • 本文由 发表于 2020年10月7日 21:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64245025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定