1

Android で http 投稿を作成するときに、次のコードを使用して URL パラメーターを作成します。

http リクエストで文字列と 2 つの文字列リストを送信したいと考えています。ただし、php コードでは、すべてのパラメーターが存在するかどうかを確認できません。以下のコードでは、php の if ステートメントが true であることを期待していますが、false です。ここで何が悪いのか誰か知っていますか?

ここの回答から、 php で一連の GET 値をループして 、パラメーターの名前を角かっこで囲み、php では配列にする方法を見ました。

ありがとう。

    List<NameValuePair> urlValues = new ArrayList<NameValuePair>();

    urlValues.add(new BasicNameValuePair("id", ID));

    for(MyLocation item : THlocationList) {
       urlValues.add(new BasicNameValuePair("THlocationList[]", item.ID));
       urlValues.add(new BasicNameValuePair("THlocationList[]", item.location));
       urlValues.add(new BasicNameValuePair("THlocationList[]", item.date));
    }
    for(String item : deletionList ) {
       urlValues.add(new BasicNameValuePair("deletionList[]", item));
    }

次に、これを使用してリクエストを行います。

public static Pair<String,Integer> GetHTTPResponse(String url, List<NameValuePair> urlparameters) {
    String result = null;
    int responseCode = 0;

    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        httppost.setEntity(new UrlEncodedFormEntity(urlparameters));
        HttpResponse response = client.execute(httppost);
        responseCode = response.getStatusLine().getStatusCode();    

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        result = rd.readLine().trim();
    }
    catch (Exception e) {
        responseCode = 0;
    }

    return new Pair<String, Integer>(result, responseCode);
}

私のphpコードでは、何らかの理由でこれがfalseと評価されています:

if (isset($_POST['id']) && isset($_POST['THlocationList']) && isset($_POST['deletionList'])) {
    ...
}
4

0 に答える 0