PHP $ _POST为空或null

时间:2013-10-21 05:46:44

标签: java php android json post

我拼命想用我的Android应用程序解决问题。我将一个列表提交到我的服务器,并将枚举设置为“标记”。 PHP页面应查看此标记,然后继续执行相关的函数并作为json数组或对象返回。这适用于一个版本的应用程序,但克隆版本无法获取数据。 PHP只是直接跳过isset标签上的标签检查,标签不是空的条件,所以看到空POST或我提交的对象不符合我不知道的某些要求时它必须是扁平的。

我查看了很多帖子并进行了搜索和搜索,但还没有找到解决方案。为什么它适用于应用程序的一个版本而不适用于升级版本,它没有对用于发送数据的方法进行任何更改?

所以这就是我正在处理的事情。首先,AsyncTask获取对象并传递给处理通信的类:

private class UpdateJobList extends AsyncTask<User, Void, Boolean>  {

    private List<Message> messages;

    public UpdateJobList()  {
        super();
        messages = new ArrayList<Message>();
    }

    @Override
    protected Boolean doInBackground(User... params){           
        try {
            CloudConnect cConn = new CloudConnect(sAddress);
            this.messages = cConn.getAll(params[0]);
            return true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result)    {
        if (true)
        {
            handleMessageList(messages);
        }
    }
}

使用CloudConnect类获取单个Json对象或对象数组:

public class CloudConnect {

private String site;
private InputStream is;
private Gson gson;

public CloudConnect(String site) throws MalformedURLException   {
    this.site = site;
    this.gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    is = null;
}


public synchronized Message get(Message m) throws IOException   {   
    Message msg = null;
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(this.site);
    post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));

    HttpResponse response = client.execute(post);
    StatusLine status = response.getStatusLine();

    if ( status.getStatusCode() == 200) {
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        try {
            Reader read = new InputStreamReader(is);
            String str = (String) gson.fromJson(read, Object.class);
            JsonParser parser = new JsonParser();
            JsonElement jElem = parser.parse(str);
            JsonObject jObject = (JsonObject) jElem;

            msg = gson.fromJson(jObject, Message.class);
            is.close();

        }   catch (Exception e) {
            e.printStackTrace();
        }
    }
    return msg;
}

public synchronized List<Message> getAll(Message m) throws IOException  {       
    List<Message> mList = new ArrayList<Message>();
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(this.site);
    post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));

    HttpResponse response = client.execute(post);
    StatusLine status = response.getStatusLine();

    if ( status.getStatusCode() == 200) {
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            JsonArray jArray = null;
            JsonReader jReader = new JsonReader(reader);
            jReader.setLenient(true);
            JsonParser parser = new JsonParser();
            if (parser.parse(jReader).isJsonArray()){
                jArray = parser.parse(jReader).getAsJsonArray();

                if ( m instanceof User ){
                    for (JsonElement je : jArray)   {
                        mList.add(gson.fromJson(je, Job.class));
                        Log.d("json", je.toString());
                    }
                } else if ( m instanceof Job ) {
                    for (JsonElement je : jArray)   {
                        mList.add(gson.fromJson(je, Update.class));
                        Log.d("json", je.toString());
                    }
                }
            }   else {
                JsonElement jElem = parser.parse(jReader);
                JsonObject jObject = (JsonObject) jElem;
                Error msg = null;
                msg = gson.fromJson(jObject, Error.class);
                mList.add(msg);
            }
            is.close();

        }   catch (Exception e) {
            e.printStackTrace();
        }
    }
    return mList;
}

检查标记的PHP代码:

if ( isset($_POST['messageType']) && $_POST['messageType'] != "") {

$tag = $_POST['messageType'];
//
//various functions depending on messageType tag here.  Such as getUser($email).
//functions appear to work fine if the PHP doesn't find the initial conditions 
//false and skips them all.
} else  {
$response["success"] = 0;
    $response["error"]["errorMsg"] = "Tags are null";
    $response["error"]["messageType"] = $tag;
            $response["error"]["varDump"] = var_dump($_POST);

echo json_encode($response);
}

1 个答案:

答案 0 :(得分:1)

好的,现在我明白了。你的Android代码没有问题,但在PHP中(由于TAG Android,我来到这里)。我不是PHP方面的专家,但请记住isset ($ _POST ['messageType']) returns true only if the payload of your POST request contains something like: messageType=some_value.因此,您需要检查传递给post.setEntity(value)的值是否为此格式的内容。

您可以使用Fiddler等工具查看请求的有效负载并进行正确调试。