JSON传输到服务器的post请求 [英] Transfer JSON to server in post request
本文介绍了JSON传输到服务器的post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
服务器有两个参数:字符串
和 JSON
。
提示,正确我转 JSON
和String在POST请求?
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(my_url);
列表参数=新的ArrayList(2);
的JSONObject的JSONObject =新的JSONObject();
jsonObject.put(par_1,1);
jsonObject.put(par_2,2);
jsonObject.put(par_3,3);
parameters.add(新BasicNameValuePair(行动,par_action));
parameters.add(新BasicNameValuePair(数据,jsonObject.toString()));
httpPost.setEntity(新UrlEn codedFormEntity(参数));
HTT presponse HTT presponse = httpClient.execute(httpPost);
Log.v(服务器应用程序,EntityUtils.toString(HTT presponse.getEntity())++ jsonObject.toString());
}赶上(UnsupportedEncodingException E){
Log.e(服务器应用程序,错误:+ E);
}赶上(ClientProtocolException E){
Log.e(服务器应用程序,错误:+ E);
}赶上(IOException异常E){
Log.e(服务器应用程序,错误:+ E);
}赶上(JSONException E){
e.printStackTrace();
}
解决方案
我不完全相信你的问题,但这里是我如何发送JSON(使用你的数据的例子)。
安卓/ JSON建设:
的JSONObject祚=新的JSONObject();
jo.put(行动,par_action);
jo.put(par_1,1);
jo.put(par_2,2);
jo.put(par_3,3);
安卓/发送JSON:
网址URL =新的URL(http://domaintoreceive.com/pagetoreceive.php);
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(url.toURI());
// prepare JSON通过设置实体发送
httpPost.setEntity(新StringEntity(jo.toString(),UTF-8));
//设置需要正确地传输JSON的头类型
httpPost.setHeader(内容类型,应用/ JSON);
httpPost.setHeader(接受编码,应用/ JSON);
httpPost.setHeader(接受语言,EN-US);
//执行POST
响应= httpClient.execute(httpPost);
PHP /服务器端:
< PHP
如果(的file_get_contents(PHP://输入')){
//获取JSON数组
$ JSON =的file_get_contents('php的://输入');
//让我们通过JSON数组解析,让我们每一个人的价值观
//在一个阵列的形式
$ parsedJSON = json_de code($ json的,真正的);
//检查以确认键设置,则定义局部变量,
//或正常不过在PHP处理你。
//如果没有设置,我们可以定义默认值
//(''在这种情况下)或做其他事
$行动=(使用isset($ parsedJSON ['行动']))? $ parsedJSON ['行动']:'';
$ 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']:'';
//或者,我们可以只使用我们的阵列是
$ SQL =更新`table` SET
`par_1` ='。$ parsedJSON ['par_1'。'
`par_2` ='。$ parsedJSON ['par_2'。'
`par_3` ='。$ parsedJSON ['par_3'。'
WHERE`action` ='$ parsedJSON。['行动'。;
}
Server takes two parameters: String
and JSON
.
Prompt, correctly I transfer JSON
and String in POST request?
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();
}
解决方案
I am not exactly sure what your issue is, but here is how I send JSON (using your data example).
Android / JSON building:
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 / Sending 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 / Server Side:
<?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'] . "'";
}
这篇关于JSON传输到服务器的post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文