gpt4 book ai didi

android - json 对象的 Volley Post 方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:21 24 4
gpt4 key购买 nike

data={
"request": {
"type": "event_and_offer",
"devicetype": "A"
},
"requestinfo": {
"value": "offer"
}
}

如何从 volley plz help 发布这个请求

            JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST,url, null ,
new Response.Listener<JSONObject>() {




@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());

msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {

/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}

js 是对我的 jsson 对象的引用......我让我的 jsson 是这样的......

JSONObject jsonobject_one = new JSONObject();
try {
jsonobject_one.put("type", "event_and_offer");
jsonobject_one.put("devicetype", "I");

JSONObject jsonobject_TWO = new JSONObject();
jsonobject_TWO.put("value", "event");
jsonobject = new JSONObject();

jsonobject.put("requestinfo", jsonobject_TWO);
jsonobject.put("request", jsonobject_one);

js = new JSONObject();
js.put("data", jsonobject.toString());
Log.e("created jsson", "" + js);

但是它没有响应该值怎么办请帮助

最佳答案

首先是你的 json 数据:

JSONObject js = new JSONObject();
try {
JSONObject jsonobject_one = new JSONObject();

jsonobject_one.put("type", "event_and_offer");
jsonobject_one.put("devicetype", "I");

JSONObject jsonobject_TWO = new JSONObject();
jsonobject_TWO.put("value", "event");
JSONObject jsonobject = new JSONObject();

jsonobject.put("requestinfo", jsonobject_TWO);
jsonobject.put("request", jsonobject_one);


js.put("data", jsonobject.toString());

}catch (JSONException e) {
e.printStackTrace();
}

然后你的json请求:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST,url, js,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());

msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {

/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}

注意标题

如果您想在本地主机上进行测试,请使用以下代码并设置您的 url 以连接您的本地主机服务器和 IP 地址:下面的代码将您的所有请求放在一个文本文件中,我试过了并且有效

<?php
file_put_contents('test.txt', file_get_contents('php://input'));
?>

关于android - json 对象的 Volley Post 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25906689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com