gpt4 book ai didi

java - 如何使用 android java 将发布数据传递给我的 php 代码?

转载 作者:行者123 更新时间:2023-11-29 19:16:54 26 4
gpt4 key购买 nike

我有以下网址和数据

网址:http://......com/hotel/api/customerapi/customer_register?APIKEY=9ffeb408h93053dc0e59a0c3f8814bcb8df30b69

输入数据格式:

{"data":{"customer_name":"ass","hotel_id":"6","mobile_no":"999966632"}}

这可能吗?如果是,我该如何传递这些数据?

我尝试了什么:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("customer_name", "ass"));
nameValuePairs.add(new BasicNameValuePair("hotel_id", "6"));
nameValuePairs.add(new BasicNameValuePair("mobile_no", "999966632"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

但是在 php 端我只能访问它"{"customer_name":"ass","hotel_id":"6","mobile_no":"999966632"}"

最佳答案

您将需要使用像 Volley 这样的东西 - here

HashMap<String, String> params = new HashMap<String, String>();
params.put("customer_name", "ass");
params.put("hotel_id", "6");
params.put("mobile_no", "999966632");

String YourURL = "http://......com/hotel/api/customerapi/customer_register?APIKEY=9ffeb408h93053dc0e59a0c3f8814bcb8df30b69";

JsonObjectRequest request_json = new JsonObjectRequest(YourURL, new JSONObject(params)),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//Do what you want on response
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//If there's an error...
}
});

//Add process to queue to get JSON in background thread
queue.add(request_json);

编辑

JsonObject jo = Json.createObjectBuilder()
.add("data", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("etc", "etc")
.add("etc", "etc")))
.build();

关于java - 如何使用 android java 将发布数据传递给我的 php 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43212951/

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