2014-11-05 46 views
1

我想使用經緯度的MultipartEntity.Values發送圖像和經度值,並將其存儲在名爲plat和plong的單獨變量中。我能夠將圖像發送到服務器,但不能同時發送經緯度。在服務器端,只有Latitude最終插入到數據庫中。 Bellow是我用來將文件和字符串發送到服務器的代碼。任何人都可以建議我在哪裏做錯了?如何使用Android的MultipartEntity發送多個字符串變量

private class UploadTask extends AsyncTask<Bitmap, Void, Void> { 

    protected Void doInBackground(Bitmap... bitmaps) { 
     if (bitmaps[0] == null) 
      return null; 
     setProgress(0); 

     Bitmap bitmap = bitmaps[0]; 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); // convert 
                    // Bitmap 
                    // to 
                    // ByteArrayOutputStream 
     InputStream in = new ByteArrayInputStream(stream.toByteArray()); // convert 
                      // ByteArrayOutputStream 
                      // to 
                      // ByteArrayInputStream 

     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     try { 
      HttpPost httppost = new HttpPost(
        "http://www.example.com/index.php"); // server 

      MultipartEntity reqEntity = new MultipartEntity(); 
      reqEntity.addPart("myFile", 
        System.currentTimeMillis() + ".jpg", in); 
      httppost.setEntity(reqEntity); 

      reqEntity.addPart("long",String.valueOf(plong)); 
      reqEntity.addPart("lat",String.valueOf(plat)); 


      Log.e(TAG, "request " + httppost.getRequestLine()); 
      HttpResponse response = null; 
      try { 
       response = httpclient.execute(httppost); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       if (response != null) 
        Log.i(TAG, "response " 
          + response.getStatusLine().toString()); 
      } finally { 

      } 
     } finally { 

     } 

     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     if (stream != null) { 
      try { 
       stream.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     return null; 
    } 

以下是MultipartEntity類中的addPart()方法。

ByteArrayOutputStream out = new ByteArrayOutputStream(); 

public void addPart(final String key, final String value) { 
    writeFirstBoundaryIfNeeds(); 
    try { 
     out.write(("Content-Disposition: form-data; name=\"" +key+"\"\r\n").getBytes()); 
     out.write("Content-Type: text/plain; charset=UTF-8\r\n".getBytes()); 
     out.write("Content-Transfer-Encoding: 8bit\r\n\r\n".getBytes()); 
     out.write(value.getBytes()); 
     out.write(("\r\n--" + boundary + "\r\n").getBytes()); 
    } catch (final IOException e) { 

    } 
} 

回答

1

嘗試使用此,

reqEntity.addPart("long",new StringBody(plong)); 
reqEntity.addPart("lat",new StringBody(plat)); 
// or plong.toString() and plat.toString() depending on your plong and plat datatype 
//and then add to httppost 
httppost.setEntity(reqEntity); 

發送lattitude和經度。

但是你可以檢查此link,好exapmles在這裏,可以是真正有助於瞭解

0

設置後添加請求的一部分,並添加new StringBody()String.valueOf()實體:

做到這一點,這可能幫助你: -

MultipartEntity reqEntity = new MultipartEntity(); 
reqEntity.addPart("myFile", 
System.currentTimeMillis() + ".jpg", in); 
reqEntity.addPart("long",new StringBody(String.valueOf(plong))); 
reqEntity.addPart("lat",new StringBody(String.valueOf(plat))); 
httppost.setEntity(reqEntity); 
+0

我試過,但我對StringBody收到錯誤要我做出StringBody類或更改爲不同的字符串方法。 – user3393926 2014-11-05 06:27:38

+0

你必須下載jar「httpmime-4.03」 – 2014-11-05 07:18:44

+0

通過這個鏈接下載jar: - http://www.java2s.com/Code/Jar/h/Downloadhttpmime411jar.htm – 2014-11-05 07:32:18

0

是@Harsh是對的。你要設置的實體....添加部分數據到multipartentity之前....

更改代碼...

 MultipartEntity reqEntity = new MultipartEntity(); 
     reqEntity.addPart("myFile", 
       System.currentTimeMillis() + ".jpg", in); 
     httppost.setEntity(reqEntity); 

     reqEntity.addPart("long",String.valueOf(plong)); 
     reqEntity.addPart("lat",String.valueOf(plat)); 

這個...

 MultipartEntity reqEntity = new MultipartEntity(); 
     reqEntity.addPart("myFile", 
       System.currentTimeMillis() + ".jpg", in); 
     //httppost.setEntity(reqEntity);//remove this line and place after setting all part values 

     reqEntity.addPart("long",new StringBody(plong)); 
     reqEntity.addPart("lat",new StringBody(plat)); 
     httppost.setEntity(reqEntity); //place it here.... 
+0

我也這樣做了,但沒有得到任何改進。 – user3393926 2014-11-05 06:25:10

+0

必須看到我更新的代碼?如果是的話...那麼你會plz..show我你的網絡服務文件....首先確保..did該服務接收經緯度和長期價值...因爲我認爲從android方面..一切都很好...所以請確保這些值由webservice接收..?如果可能,請告訴我你的服務檔案? – 2014-11-05 07:35:09

相關問題