2014-04-21 66 views
-1

我的代碼正在爲一個圖像,當我上傳多個圖像時,它顯示錯誤給我。如何發送多個圖像到服務器

任何人都可以幫助我解決這個問題嗎?

使用下面的代碼我可以上傳單個圖像,但我想上傳多個圖像,請幫助我如何上傳圖像。 感謝

這裏是我的代碼

String image_path=imageUrl1; 
     GetData(myFiles);   

} 


     }); 

} 


public void GetData(String myFiles) 
{ 

     String image_retrive_url="http://tradewale.com/open/tradedata.php?submitType=PostAd"; 

     pDialog = new ProgressDialog(PostAddActivity.this); 
     pDialog.setMessage(" Sending..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 




    Bitmap bitmap = BitmapFactory.decodeFile(myFiles); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream); 
    byte [] byte_arr = stream.toByteArray(); 
    String image_str = Base64.encodeBytes(byte_arr); 
    final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

    String Title = title.getText().toString(); 
    String Description = description.getText().toString().trim(); 
    String PhoneNumber = phonenumber.getText().toString(); 
    String Email = email.getText().toString(); 
    String Price = price.getText().toString(); 


    nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

    nameValuePairs.add(new BasicNameValuePair("photo_url", image_retrive_url)); 



    Thread t = new Thread(new Runnable() 
    { 

    @Override 
    public void run() { 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://tradewale.com/open/tradedata.php?submitType=PostAd"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       final String the_string_response = convertResponseToString(response); 


      } 
     catch(final Exception e) 
      { 
       runOnUiThread(new Runnable() 
       { 

        @Override 
        public void run() 
        { 
         Toast.makeText(PostAddActivity.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();        
        } 
       }); 
        System.out.println("Error in http connection "+e.toString()); 
      } 
    } 
     }); 
    t.start(); 
    } 

回答

0

要在Web服務發送更多的圖像。你也必須改變webservice。在服務部分添加one more參數。所以您可以一次發送多個圖像。

,並得到像e.g. img_str2

base64串第二圖像:

String image_str = Base64.encodeBytes(byte_arr); 
String image_str2 = Base64.encodeBytes(byte_arr2); 

,並命名爲新的參數在服務像image2

nameValuePairs.add(new BasicNameValuePair("image",image_str)); 
nameValuePairs.add(new BasicNameValuePair("image2",image_str2)); 

這裏imageimage22 diff。圖像參數。這些參數名稱也必須爲define in your service side code ..以便服務代碼可以保存您的內容。

+0

以這種方式我創建了四種不同的方法 – user3492351

+0

不需要四種不同的方法..只需添加更多的參數。 –

+0

如何做到這一點,你可以告訴我 – user3492351