2012-12-11 75 views
0

同時發送來自Android的數據和圖像我曾經發送成功的數據和圖像之前,但它是用兩種不同的方法如何通過PHP

做,這是我的代碼發送數據

公共類HTTPPostData延伸的AsyncTask {

@Override 
    protected String doInBackground(String... urls) { 
     String Result = ""; 
     byte[] Bresult = null; 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(URL_TO_LOAD); 
     try { 
      List<NameValuePair> nameValuePairs = LPD; 
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 
      HttpResponse response = client.execute(post); 
      StatusLine statusLine = response.getStatusLine(); 
      if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) { 
       Bresult = EntityUtils.toByteArray(response.getEntity()); 
       Result = new String(Bresult, "UTF-8"); 
      } 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
     } 
     return Result; 
    } 

    protected void onPostExecute(String result) { 
     // dismiss the dialog after the file was downloaded 
     if (!result.toString().trim().equals("")) { 
      RunProcedure.StrParam = result; 
      RunProcedure.run(); 
     } 
    } 
} 

,這我的代碼轉移PIC

public boolean TransferFileToHttp(String address_to_handle, String file_name) { 
    boolean result = false; 
    HttpURLConnection connection = null; 
    DataOutputStream outputStream = null; 
    // DataInputStream inputStream = null; 

    String pathToOurFile = file_name; 
    String urlServer = address_to_handle; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 

    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 

    try { 
     FileInputStream fileInputStream = new FileInputStream(new File(
       pathToOurFile)); 

     URL url = new URL(urlServer); 
     connection = (HttpURLConnection) url.openConnection(); 

     // Allow Inputs & Outputs 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 

     // Enable POST method 
     connection.setRequestMethod("POST"); 

     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("Content-Type", 
       "multipart/form-data;boundary=" + boundary); 

     outputStream = new DataOutputStream(connection.getOutputStream()); 
     outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
     outputStream 
       .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" 
         + pathToOurFile + "\"" + lineEnd); 
     outputStream.writeBytes(lineEnd); 

     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 

     // Read file 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

     while (bytesRead > 0) { 
      outputStream.write(buffer, 0, bufferSize); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     } 

     outputStream.writeBytes(lineEnd); 
     outputStream.writeBytes(twoHyphens + boundary + twoHyphens 
       + lineEnd); 

     // Responses from the server (code and message) 
     int serverResponseCode = connection.getResponseCode(); 
     String serverResponseMessage = connection.getResponseMessage(); 

     fileInputStream.close(); 
     outputStream.flush(); 
     outputStream.close(); 
     result = true; 
    } catch (Exception ex) { 
     // Exception handling 
     result = false; 
    } 
    return result; 
} 

如何將傳輸文件過程加入發佈數據過程並檢索字符串?

+1

你的標題說「...通過php」?我在你的問題中沒有看到任何與PHP相關的內容? – Don

+0

我通過android發送php url – AsepRoro

回答

1

這是完全可能的。但是,您將不得不執行一些額外的步驟。

您將首先必須將圖像轉換爲基本64字符串。參考此文檔 http://developer.android.com/reference/android/util/Base64.html

現在可以將字符串作爲常規json數據發送。

在服務器端,您需要一種機制將base64字符串轉換爲圖像。這是一項微不足道的任務。

這種方法存在一些缺點,例如json請求的巨大尺寸和額外的編碼/解碼開銷。

+0

這樣做的最佳方法是什麼? – Krishnan

+0

你目前使用的是一種好方法。要解決上傳確認的問題,你可以做的是先上傳圖片,然後上傳json。你的json應該有圖像路徑,並且php應該驗證那些以確保圖像到位。爲了進一步確認,您還可以使用圖像的md5散列。 –