我用这code发送这对我的PHP文件。 该文件看起来什么是未来是这样的。
file_put_contents(dump.txt,POST:\ N的print_r($ _ POST,真)\ñ\ñ\ñGET:\ N。的print_r( $ _GET,真));
我发送的JSON是这样的:
公共无效POSTDATA(字符串URL,JSONObject的JSON){
HttpClient的HttpClient的=新DefaultHttpClient();
String对象= + json.toString()数据=?;
的System.out.println(对象:+对象);
尝试 {
HttpPost httppost =新HttpPost(url.toString());
httppost.setHeader(内容型,应用/ JSON);
的System.out.println(URL:+ url.toString());
StringEntity SE =新StringEntity(对象);
的System.out.println(JSON:+ json.toString());
se.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE,应用/ JSON));
httppost.setEntity(SE);
HTT presponse响应= httpclient.execute(httppost);
字符串临时= EntityUtils.toString(response.getEntity());
的System.out.println(回应:+响应);
。INT状态code = response.getStatusLine()的getStatus code();
的System.out.println(状态code+状态code);
如果(状态code == 200){
Toast.makeText(LbsGeocodingActivity.thisVerstuurd,Toast.LENGTH_LONG).show();
}
否则,如果(状态code!= 200){
Toast.makeText(这一点,状态code,Toast.LENGTH_LONG).show();
}
/* 尝试 {
串responseBody = EntityUtils.toString(response.getEntity());
Toast.makeText(这一点,responseBody,Toast.LENGTH_LONG).show();
}赶上(ParseException的E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
} * /
}
赶上(ClientProtocolException E){
}
赶上(IOException异常E){
}
}
日志文件是这样的:
九月1日至8日:36:00.278:我/的System.out(1124):对象:数据= {ID:69403,时间戳:08 -01-2013 9时36分00秒,经度: - 122.084095,纬度:37.422005}
9月1日至八日:36:00.298:我/的System.out(1124):JSON:{ID:69403,时间戳:2013年8月1日9时36分00秒,东经: -122.084095,纬度:37.422005}
9月1日至八日:36:01.038:我/的System.out(1124):回应:org.apache.http.message.BasicHtt$p$psponse@412d1b38
9月1日至八日:36:01.038:我/的System.out(1124):状态code 200
我离开了URL出于安全考虑。
但我正在逐渐的dump.txt一个空文件
邮编:
排列
(
)
得到:
排列
(
)
修改
东西是错误的setEntity(或某处身边有),因为当我已经粘贴数据=在URL中我得到这个从PHP文件:?
邮编:
排列
(
)
得到:
排列
(
[数据] =>
)
解决方案
我在我的应用程序工作发送JSON和我的 DefaultHttpClient
完美的方式:
公共静态HttpUriRequest createPostForJSONObject(
JSONObject的参数,可以字符串URL){
HttpPost后=新HttpPost(URL);
post.setEntity(createStringEntity(PARAMS));
返回岗位;
}
私有静态HttpEntity createStringEntity(JSONObject的PARAMS){
StringEntity SE = NULL;
尝试 {
本身=新StringEntity(params.toString(),UTF-8);
se.setContentType(应用/ JSON的;字符集= UTF-8);
}赶上(UnsupportedEncodingException E){
Log.e(TAG,无法创建StringEntity,E);
异常= E;
}
返回本身;
}
为了运行在code此要求所有你需要做的是这样的:
公共无效POSTDATA(字符串URL,JSONObject的JSON){
HttpClient的HttpClient的=新DefaultHttpClient();
尝试 {
HttpPost后= createPostForJSONObject(JSON,URL);
HTT presponse响应= httpClient.execute(后);
//做你的事
} catch异常{
//处理异常
}
}
I am using this code to send this to my php file. The file looks what is coming like this.
file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));
I am sending the json like this:
public void postData(String url,JSONObject json) {
HttpClient httpclient = new DefaultHttpClient();
String object = "?data=" +json.toString();
System.out.println("object:" + object);
try {
HttpPost httppost = new HttpPost(url.toString());
httppost.setHeader("Content-type", "application/json");
System.out.println("url:" + url.toString());
StringEntity se = new StringEntity(object);
System.out.println("json:" + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
System.out.println("response:" + response);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Statuscode " + statusCode);
if(statusCode==200) {
Toast.makeText(LbsGeocodingActivity.this, "Verstuurd", Toast.LENGTH_LONG).show();
}
else if(statusCode!=200) {
Toast.makeText(this, statusCode, Toast.LENGTH_LONG).show();
}
/* try {
String responseBody = EntityUtils.toString(response.getEntity());
Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
catch (ClientProtocolException e) {
}
catch (IOException e) {
}
}
The log files are this:
01-08 09:36:00.278: I/System.out(1124): object:?data={"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"}
01-08 09:36:00.298: I/System.out(1124): json:{"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"}
01-08 09:36:01.038: I/System.out(1124): response:org.apache.http.message.BasicHttpResponse@412d1b38
01-08 09:36:01.038: I/System.out(1124): Statuscode 200
i left out the url for security reasons..
But i am getting an empty file in dump.txt
POST:
Array
(
)
GET:
Array
(
)
EDIT
Something is wrong with the setEntity(or somewhere around there), because when i already paste ?data= in the url i get this from the PHP file:
POST:
Array
(
)
GET:
Array
(
[data] =>
)
解决方案
the way I send JSON with my DefaultHttpClient
in my app work perfectly:
public static HttpUriRequest createPostForJSONObject(
JSONObject params, String url) {
HttpPost post = new HttpPost(url);
post.setEntity(createStringEntity(params));
return post;
}
private static HttpEntity createStringEntity(JSONObject params) {
StringEntity se = null;
try {
se = new StringEntity(params.toString(), "UTF-8");
se.setContentType("application/json; charset=UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Failed to create StringEntity", e);
exception = e;
}
return se;
}
in order to run this request in your code all you need to do is this:
public void postData(String url,JSONObject json) {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost post = createPostForJSONObject(json, url);
HttpResponse response = httpClient.execute(post);
// DO YOUR THING
} catch Exception {
// HANDLE EXCEPTION
}
}