json对象发布到Android应用程序中的PHP脚本(json object post to php script in android app)
我试过这个android代码发送json对象到我的网站
HttpPost httppost = new HttpPost(url); JSONObject j = new JSONObject(); j.put("name","name "); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); String format = s.format(new Date()); nameValuePairs.add(new BasicNameValuePair("msg",j.toString() )); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost);
而这个PHP代码
<?php $msg=$_POST["msg"]; $filename="androidmessages.html"; file_put_contents($filename,$msg."<br />",FILE_APPEND); $androidmessages=file_get_contents($filename); echo $androidmessages; ?>
它会告诉我{“name”:“name”}但如果我使用的话
httppost.setHeader( "Content-Type", "application/json" );
它没有显示任何内容。我之前没有关于json对象帖子的经验,但我认为出了问题。我想将一些用户信息发送到我的网站并在网页上显示它可以告诉我我需要改变什么才能克服这个问题问题谢谢
I tried this android code for send json objects to my website
HttpPost httppost = new HttpPost(url); JSONObject j = new JSONObject(); j.put("name","name "); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); String format = s.format(new Date()); nameValuePairs.add(new BasicNameValuePair("msg",j.toString() )); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost);
And this php code
<?php $msg=$_POST["msg"]; $filename="androidmessages.html"; file_put_contents($filename,$msg."<br />",FILE_APPEND); $androidmessages=file_get_contents($filename); echo $androidmessages; ?>
It will show me {"name":"name "} but if i use
httppost.setHeader( "Content-Type", "application/json" );
it will show nothing.I have no before experince about json object post but i think something went wrong.I want to send some user information to my website and display it in web page can you please tell me what i need to change to overcome this problem Thank you
原文:https://stackoverflow.com/questions/17896232
最满意答案
凯文解释了问题所在,并且你的假设是正确的。 对于这样的功能类,你可以应用一个简单的修复:
time_t now; struct tm local_time; now = time(NULL); local_time = *localtime(&now);
从
localtime(&now)
返回时,静态分配的结构的内容将被复制到本地结构local_time
。编辑:还有很多其他的事情要说:
- 使用线程之间的函数仍然有问题...
- 在许多实现中,在
mktime
和gmtime
之间共享相同的缓冲区,所以也可以使用这些缓冲区来修改结构。C99和C11提供更安全的功能[TR 24731-1]。
localtime_s(const time_t * restrict timer, struct tm * restrict dst);
Kevin explained what the problem is, and that your assumption is correct. For such classes of functions you could apply a simple fixing:
time_t now; struct tm local_time; now = time(NULL); local_time = *localtime(&now);
On return from
localtime(&now)
the contents of the statically allocated structure will be copied on the local structurelocal_time
.EDIT: There are many other things to say:
- You still have a problem using the function between threads...
- The same buffer is shared, on many implementations, between
mktime
andgmtime
, so also using these can modify the structure.C99 & C11 offer a safer function [TR 24731-1].
localtime_s(const time_t * restrict timer, struct tm * restrict dst);
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
分配内存来保存字符。 #include
#include typedef struct PString { char *chars; int (*length)(); } PString; int length(PString *self) { return strlen(self->chars); } PString *initializeString(int n) { PString *str = mallo ... -
在C中,我将如何选择是否返回结构体或指向结构体的指针?(In C, how would I choose whether to return a struct or a pointer to a struct?)[2024-01-02]
当something_t很小(读取:复制它与复制指针一样便宜),并且您希望它默认为堆栈分配: something_t make_something(void); something_t stack_thing = make_something(); something_t *heap_thing = malloc(sizeof *heap_thing); *heap_thing = make_something(); 当something_t很大或者你想要它被堆分配: something_t *mak ... -
你的赋值start = tmp只改变addtoken()里面的start值。 由于指针是按值传递的,因此不会改变函数外部的指针start 。 为了实现这一点,你必须将一个指向指针的指针传递给你的函数: void addtoken(char* word, int counter, struct shellvalue** start, struct shellvalue** current) { // .. // replace assignments to start/current with ...
-
从函数返回的结构的C ++指针导致访问冲突[重复](C++ pointer to struct returned from function causes access violation [duplicate])[2022-03-27]
GeneticData geneticData = *new GeneticData(); 这个怪物动态地分配一个对象,使用它来复制初始化一个局部变量,然后扔掉唯一指向它的指针,导致内存泄漏。 return &geneticData; 这将返回一个指向局部变量的指针,该局部变量在调用者可以对其执行任何操作之前销毁; 导致你的错误。 也许你想要返回动态对象(记得在你使用完它后删除它): GeneticData * geneticData = new GeneticData(); //... return ... -
typedef struct llist { int valor; struct lligada *prox; } *LInt; 使LInt等同于struct llist* (包括星号)。 在C中我遇到的所有现代资源中都非常不鼓励这种类型定义指针的做法,但它在历史上已被使用,特别是lcc编译器经常使用这种做法(它们的约定也大写了指针类型定义的名称)。 typedefing指针的问题在于它可能会让人感到困惑,你可以通过它们突然传递0(作为NULL),但是如果你有一个命名约定(例如大写每个指针 ...
-
// Return a pointer to our struct. return fillInventory; } 您的结构对象在您的函数中声明并具有在函数返回后结束的生命周期。 当你的函数返回并试图访问它时调用未定义的行为,结构对象将被丢弃。 将结构对象作为参数传递给函数,或者使用malloc分配其存储空间以解决问题。 // Return a pointer to our struct. return fillInventory; } Your structure objec ...
-
凯文解释了问题所在,并且你的假设是正确的。 对于这样的功能类,你可以应用一个简单的修复: time_t now; struct tm local_time; now = time(NULL); local_time = *localtime(&now); 从localtime(&now)返回时,静态分配的结构的内容将被复制到本地结构local_time 。 编辑:还有很多其他的事情要说: 使用线程之间的函数仍然有问题... 在许多实现中,在mktime和gmtime之间共享相同的缓冲区,所以也可以使用这 ...
-
这只是一个运营商优先问题。 编译器进程 - >在*之前。 因此,它试图访问struct hashtable **的大小成员,这是不可能的。 如果你用(* ht) - > size交换* ht-> size,代码就会编译。 This is just a operator precedence problem. The compiler processes -> before the *. Therefore, it tries to access the size member of struct hasht ...
-
当你想处理指针时,你必须引入动态内存分配。 创建指针意味着您告诉编译器'我将在其中存储变量的地址'。 因此,在这种情况下,要么为其创建对象分配地址,要么动态地为其分配内存。 情况1: main() { struct A *a; struct A obj;//assign address of other object manually a = &obj; a->val1 = 1; a->val2 = 2; calc((void*)a); // calc return ...