1

わかりません_POSTが空です

JSONOBJECTを作成し、次のようにHTTPPostに渡します。

   HttpPost post = new HttpPost(URL);

                StringEntity se = new StringEntity(json.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                post.setEntity(se);
                response = client.execute(post);

それでも、空の配列として表示されます

これが完全なコードです

JSONオブジェクトを作成します(これをデバッグして、埋めることができます

public static final JSONObject GetJSONObject(Customer customer) {

        try {
            JSONObject values = new JSONObject();
            values.put(Customer.KEY_NAME, customer._name); // Contact Name
            values.put(Customer.KEY_PH_NO, customer._phone); // Contact Phone
            values.put(Customer.KEY_EMAIL, customer._email);
            values.put(Customer.KEY_UUID, customer._UUID);
            values.put(Customer.KEY_GEOUUID, customer._geoUUID);
            values.put(Customer.KEY_SALESREPUUID, customer._salesRepUUID);
            values.put(Customer.KEY_CUST_LAST_UPDATE, customer.last_update);
            values.put(Customer.KEY_CUST_NOTES, customer._notes);
            return values;
        } catch (JSONException e) {
            Log.d("ERROR", e.getMessage());
            System.exit(1);
        }
        return null;
    }

スレッドの送信を開始します

        ma.ShowToastMsg("Updating Contract To mySQL" + customer._name);
    mh.sendJSONTHREAD("http://.php",
            GetJSONObject(customer));
public void sendJSONTHREAD(final String URL, final JSONObject json) {
    new Thread(new Runnable() {
        public void run() {
            sendJson(URL, json);
        }
    }).start();
}

それを送る

public void sendJson(final String URL, final JSONObject json) {
    Looper.prepare(); // For Preparing Message Pool for the child Thread
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
                                                                            // Limit
    HttpResponse response;

    try {
        HttpPost post = new HttpPost(URL);

        StringEntity se = new StringEntity(json.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        post.setEntity(se);
        response = client.execute(post);


        /* Checking response */
        if (response != null) {
            Log.d("RESPONSE", EntityUtils.toString(response.getEntity()));

            //InputStream in = response.getEntity().getContent(); // Get the
                                                                // data in
                                                                // the
                                                                // entity
            //Log.d("HTTPPOST", in.toString());
        }

    } catch (Exception e) {
        e.printStackTrace();
        // createDialog("Error", "Cannot Estabilish Connection");
    }

    Looper.loop(); // Loop in the message queue

}

そして物事のPHP側

include("settings.php");

mysql_connect($loginURL,$username,$password);
@mysql_select_db($database) or die("-9");

echo var_dump($_POST);

$varname = mysql_real_escape_string(strip_tags($_POST['name']));
$varemail = mysql_real_escape_string(strip_tags($_POST['email']));
$varphone = mysql_real_escape_string(strip_tags($_POST['phone']));
$varUUID = mysql_real_escape_string(strip_tags($_POST['UUID']));
$vargeopointUUID = $_POST['geopointUUID'];

echo var_dump($_POST);

echo var_dump($ _ POST)-array(0){}を返します

4

1 に答える 1

4

POSTは、JSONが渡されることを想定していませんが、次のような名前と値のペアを想定しています。

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("key1", "value1"));
params.add(new BasicNameValuePair("key2", "value2"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

JSONをphpに渡したい場合は、を調べて解析する必要があります$HTTP_RAW_POST_DATA

生の過去のデータを解析するための1つのアプローチ(OPによって引用)は次のとおりです(php):

$obj = json_decode($HTTP_RAW_POST_DATA); $varname =$obj->{'name'};
于 2013-02-12T03:22:42.427 に答える