duanruinong0619 2013-10-21 05:46
浏览 50

PHP $ _POST为空或null

I'm desperately trying to solve an issue with my android app. I submit a List to my server with an enum set as a 'tag'. The PHP pages should look at this tag and then proceed to perform the associated functions and return as a json array or object. This works fine with one version of the app but a cloned version fails to fetch data. The PHP just jumps straight over tag checking at the isset tag and tag is not empty conditions so it must be flat out seeing an empty POST or the object I submit doesn't meet some requirement I'm unaware of.

I've looked through so many posts and searched and searched but haven't found a solution. Why would it work for one version of the app but not for the upgraded version, that hasn't made any changes to the methods used for sending data??

So here's what I'm dealing with. To begin with, an AsyncTask takes the objects and passes to a class that handles communication:

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);
        }
    }
}

Using the CloudConnect class to get single Json objects or an array of Objects:

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;
}

And the PHP code that checks for the tag:

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条回答 默认 最新

  • dongyou6768 2013-10-24 03:05
    关注

    Ok, now I get it. You do not have a problem on your Android code but in PHP (I came here because of the TAG Android). I am not an expert in PHP, but remember that isset ($ _POST ['messageType']) returns true only if the payload of your POST request contains something like: messageType=some_value. So you need to check if the value that you passsed to post.setEntity(value) is something in this format.

    You can use tools like Fiddler to see the payload of your request and debug properly.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭