定制型Htt presponse于Android到PHP一个POST之后 [英] custom HttpResponse after a POST from Android to PHP

查看:210
本文介绍了定制型Htt presponse于Android到PHP一个POST之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有perfom一个POST到PHP页面类,这个页面应该与数据发布的一些动作,并返回结果(像OK,KO)。我怎样才能把结果发送回PHP,以及如何使用它在android系统?

I have a class that perfom a POST to a php page, this page should make some action with the posted data, and return the result (something like OK, KO). how can I send the result back in PHP, and how I use it in android?

在Android身边,我已经得到了这一点,这发布数据:

on the Android side i've got this, which post the data:

public int postData(String id, String webrispid, String userid) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.server.com/postpage.php");
            int responseCode = 0;
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id", id));               
                nameValuePairs.add(new BasicNameValuePair("user", userid));
                nameValuePairs.add(new BasicNameValuePair("webrisid", webrispid));
                try {
                    nameValuePairs.add(new BasicNameValuePair("token",AeSimpleSHA1.SHA1(userid) ));
                } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                responseCode = response.getStatusLine().getStatusCode();
                System.out.println(response);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            return responseCode;
        }

和实际的postpage.php看起来是这样的:

and actually the postpage.php looks like this:

<?php

if ( isset($_POST['user']) ) {
// try to store the data posted

//if data is stored return "ok"

// else return "ko"

} else {
// error
}
?>

所以在Android方面,我应该停止发送相同的数据并开始处理数据的其余部分,并最终将其发送到网页...

so on the android side, i should stop sending the same data and start process the rest of data and eventually send it to the page...

推荐答案

如果它仅仅是OK或KO,你可以做到以下几点:

If it is just 'OK' or 'KO', you could do the following:

<?php
  if ( isset($_POST['user']) ) { // try to store the data posted
    //do some test
    echo "OK"; //or KO
  } else { 
    echo "ERROR"; 
  }
?>

Android的部分看起来是这样的:

The android part could look like this:

// Execute HTTP Post Request
String status = httpclient.execute(httpPost, new BasicResponseHandler());
System.out.println(status); //will be OK or KO or ERROR

这篇关于定制型Htt presponse于Android到PHP一个POST之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆