2012-02-09 19 views
5

私はこれのNameValuePairメソッドを使用して、私のAndroidのクライアントからWebサーバへの値のカップルを送信したい:NameValuePairを使用してバイトHTTPを送信する方法

public void postData() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http:/xxxxxxx"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     String amount = paymentAmount.getText().toString(); 
     String email = inputEmail.getText().toString(); 
     nameValuePairs.add(new BasicNameValuePair("donationAmount", amount)); 
     nameValuePairs.add(new BasicNameValuePair("email", email)); 
     nameValuePairs.add(new BasicNameValuePair("paymentMethod", "5")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

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

あいにくのNameValuePairは、文字列を送信することができるだけで、私もバイト[]の値を送信する必要があります。誰も私の問題を解決するために私を助けることができますか?この例のイメージ(JPG)を掲載イムと文字列の中

+2

エンコード 'バイト[]'をBase64文字列または私は画像を送信する必要がある他の 'HttpEntity'例えば' MultipartEntity'(サードパーティ製のlibには必要とされている...ちょうどそれをグーグル) – Selvin

+0

を使用しています。 – Selva

+0

webservice.iで処理する方法をvb.netを使用してasmxを使用してBase64の文字列を送信した場合 – Selva

答えて

5
 HttpPost httppost = new HttpPost("http://upload-test.php"); 
     MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     HttpClient httpClient = new DefaultHttpClient(); 
     if(bm!=null){ 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bm.compress(CompressFormat.JPEG, 75, bos); 
      byte[] data = bos.toByteArray(); 
      ByteArrayBody bab = new ByteArrayBody(data, name+".jpg"); 
      entity.addPart("file", bab); 
     } 
     try { 
      StringBody sname = new StringBody(name); 
      entity.addPart("name", sname); 


     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     httppost.setEntity(entity); 
     try { 
      httpClient.execute(httppost); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

あなたはここにマルチポストライブラリをダウンロードすることができます:http://hc.apache.org/downloads.cgi BMがビットマップです。 また、使用することができます。

Bundle bundle=new Bundle(); 
bundle.putString("key", "value"); 
byte[] b = bundle.getByteArray("key"); 
ByteArrayBody bab = new ByteArrayBody(b,"info"); 
1

エンコードバイトをStringに:文字列BA1 = Base64.encodeBytes(BA);

 Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath); 
     ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

     //Resize the image 
     double width = bitmapOrg.getWidth(); 
     double height = bitmapOrg.getHeight(); 
     double ratio = 400/width; 
     int newheight = (int)(ratio * height); 

     System.out.println("———-width" + width); 
     System.out.println("———-height" + height); 

     bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true); 

     //Here you can define .PNG as well 
     bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao); 
     byte[] ba = bao.toByteArray(); 
     String ba1 = Base64.encodeBytes(ba); 

     System.out.println("uploading image now ---" + ba1); 
     String a = "aaaaa"; 

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("image", ba1)); 
     nameValuePairs.add(new BasicNameValuePair("a", a)); 

     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2:8080/upload_test/upload_image.php"); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity();    

      // print responce 
      outPut = EntityUtils.toString(entity); 
      Log.i("GET RESPONSE—-", outPut); 

      //is = entity.getContent(); 
      Log.e("log_tag ******", "good connection"); 

      bitmapOrg.recycle(); 
     }catch (Exception e) { 
      Log.e("log_tag ******", "Error in http connection " + e.toString()); 
     } 
+0

これは変更サーバー側が必要です。私には適していません。 –

関連する問題