1

我有一个类执行 POST 到 php 页面,该页面应该对发布的数据进行一些操作,并返回结果(类似于 OK,KO)。如何在 PHP 中将结果发送回,以及如何在 android 中使用它?

在Android方面,我有这个,它发布数据:

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 看起来像这样:

<?php

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

//if data is stored return "ok"

// else return "ko"

} else {
// error
}
?>

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

4

3 回答 3

2

如果只是“OK”或“KO”,您可以执行以下操作:

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

android 部分可能如下所示:

// Execute HTTP Post Request
String status = httpclient.execute(httpPost, new BasicResponseHandler());
System.out.println(status); //will be OK or KO or ERROR
于 2013-01-08T10:53:40.153 回答
0

当您在操作结束时从 PHP 文件返回时,请使用

echo  json_encode("return status or some specific value");` 

在 Android 端,解析它json以获得状态结果。

于 2013-01-08T10:47:57.017 回答
0

看到这个:

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);
            HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);

                    String status = jObj.getString("status");   




    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }




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

这里的“状态”是使用 echo json_encode() 函数从 php 文件返回的 jsonObject。

于 2013-01-08T10:56:11.073 回答