2011-08-17 10 views
1

HttpConnectionのものを使って、PHPファイルに変数Stringを投稿したいと思います。最初のデータは、レコードストアから返されたbyte []データです。したがって、それは単独で投稿する必要があります。だからどのように文字列変数を投稿する?名前付き変数をPHPファイルに投稿するには?

+0

あなたが助けやろうとしているかについて例を追加何が必要なのかを理解する。説明はあまりにも並べ替えです。 – Dubas

+0

@Dubas:例:byte [] recorstoreレコードと値 "/ MyDirectory"を含むString変数をPHPファイル(url = http://192.168.1.123/myproject/uploads/treatphoto.php)に投稿したいのですが、 。 –

+0

2つのパラメータを渡すことができます。 1つはbyte []で、もう1つは文字列名です。そしてあなたはサーバー側でそれを得ることができます。 – bharath

答えて

2

GETまたはPOSTメソッドを使用して、データをPHPファイルに渡すことができます。

取得メソッドは、簡単なデータを渡す簡単な方法です。

192.168.1.123/myproject/uploads/treatphoto.php?myVariable1=MyContent&myVariable2=MyContent2 

そしてPHPで:あなたが例のURLに

を変数を追加することができますGET使用

$content1 = $_GET['myVariable1']; 
$content2 = $_GET['myVariable2']; 

も ​​"MyContent" の内容は、エンコードされた文字列である必要があります。任意のUrlEncoderを使用します。

バイトを渡すには、[]配列あなたはbase64で

のようないくつかの印刷可能なエンコーディングでエンコードされた文字列にバイト配列を変換する必要があり、このメソッドを使用してGETメソッドでも安全に渡すことができるデータの並べ替えの制限があります(通常は2048バイト)

もう1つの方法 "POST"はより複雑です(ただし、多くはありません)。データを追加する方法です。

データをPOSTとして渡すには、HttpConnectionを準備する必要があります。 また、urlParamentersに格納されているデータは、URLのエンコンディングに従っている必要があります。 投稿を使用してデータを渡すことはGETに似ていますが、URLの隣にすべての変数を追加する代わりに、httpuiConnectionリクエストのストリームにvaruiablesが追加されます。 Javaコードの

例:

String urlParameters = "myVariable1=myValue1&myVariable2=myValue2"; 

HttpURLConnection connection = null; 
try { 
    url = new URL(targetURL); 
    connection = (HttpURLConnection)url.openConnection(); 

    // Use post and add the type of post data as URLENCODED 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 

    // Optinally add the language and the data content 
    connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); 
    connection.setRequestProperty("Content-Language", "en-US"); 

    // Set the mode as output and disable cache. 
    connection.setUseCaches (false); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 

    //Send request 
    DataOutputStream wr = new DataOutputStream (connection.getOutputStream()); 
    wr.writeBytes (urlParameters); 
    wr.flush(); 
    wr.close(); 


    // Get Response  
    // Optionally you can get the response of php call. 
    InputStream is = connection.getInputStream(); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    String line; 
    StringBuffer response = new StringBuffer(); 
    while((line = rd.readLine()) != null) { 
    response.append(line); 
    response.append('\r'); 
    } 
    rd.close(); 
    return response.toString(); 

PHPは似ている、あなただけの$ _POSTで$ _GETを交換する必要があります。

$content1 = $_POST['myVariable1']; 
$content2 = $_POST['myVariable2']; 
関連する問題