gpt4 book ai didi

android - 在 post 请求中将 JSON 传输到服务器

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:05 24 4
gpt4 key购买 nike

服务器有两个参数:StringJSON。提示,我在POST请求中正确传输了JSON和String?

try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("my_url");
List parameters = new ArrayList(2);
JSONObject jsonObject = new JSONObject();
jsonObject.put("par_1", "1");
jsonObject.put("par_2", "2");
jsonObject.put("par_3", "3");
parameters.add(new BasicNameValuePair("action", "par_action"));
parameters.add(new BasicNameValuePair("data", jsonObject.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.v("Server Application", EntityUtils.toString(httpResponse.getEntity())+" "+jsonObject.toString());

} catch (UnsupportedEncodingException e) {
Log.e("Server Application", "Error: " + e);
} catch (ClientProtocolException e) {
Log.e("Server Application", "Error: " + e);
} catch (IOException e) {
Log.e("Server Application", "Error: " + e);
} catch (JSONException e) {
e.printStackTrace();
}

最佳答案

我不确定您的问题是什么,但这是我发送 JSON 的方式(使用您的数据示例)。

Android/JSON 构建:

JSONObject jo = new JSONObject();
jo.put("action", "par_action");
jo.put("par_1", "1");
jo.put("par_2", "2");
jo.put("par_3", "3");

Android/发送 JSON:

URL url = new URL("http://domaintoreceive.com/pagetoreceive.php");

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url.toURI());

// Prepare JSON to send by setting the entity
httpPost.setEntity(new StringEntity(jo.toString(), "UTF-8"));

// Set up the header types needed to properly transfer JSON
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
httpPost.setHeader("Accept-Language", "en-US");

// Execute POST
response = httpClient.execute(httpPost);

PHP/服务器端:

<?php
if (file_get_contents('php://input')) {
// Get the JSON Array
$json = file_get_contents('php://input');
// Lets parse through the JSON Array and get our individual values
// in the form of an array
$parsedJSON = json_decode($json, true);

// Check to verify keys are set then define local variable,
// or handle however you would normally in PHP.
// If it isn't set we can either define a default value
// ('' in this case) or do something else
$action = (isset($parsedJSON['action'])) ? $parsedJSON['action'] : '';
$par_1 = (isset($parsedJSON['par_1'])) ? $parsedJSON['par_1'] : '';
$par_2 = (isset($parsedJSON['par_2'])) ? $parsedJSON['par_2'] : '';
$par_3 = (isset($parsedJSON['par_3'])) ? $parsedJSON['par_3'] : '';

// Or we could just use the array we have as is
$sql = "UPDATE `table` SET
`par_1` = '" . $parsedJSON['par_1'] . "',
`par_2` = '" . $parsedJSON['par_2'] . "',
`par_3` = '" . $parsedJSON['par_3'] . "'
WHERE `action` = '" . $parsedJSON['action'] . "'";
}

关于android - 在 post 请求中将 JSON 传输到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16112277/

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