2016-10-03 24 views
0

我試圖使用排球庫上傳圖像,我不知道如何。如何上傳圖像文件到服務器以及我的字符串參數使用Volley

我已經提到以前提出的問題,但似乎沒有滿足。

這是我需要的是什麼acheive:

enter image description here

下面是我嘗試使用代碼:

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        Log.d("**********", " Response Received is " + response); 

        try { 

         JSONObject jsonObject = new JSONObject(response); 

         Log.d("**********", "STRING TO JSON CONVERSION DONE , IT IS " + jsonObject.toString()); 

         ParseReqOtp parseReqOtp = new ParseReqOtp(); 
         parseReqOtp.parseImageupload(jsonObject); 


        } catch (Exception e) { 
         Log.d("**********", "ERROR IN STRING TO JSON CONVERSION  " + e.toString()); 

        } 
        Log.d("**********", "FETCHING IN VOLLEY REQ" + response); 




       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(AddPic.this, error.toString(), Toast.LENGTH_LONG).show(); 
       } 
      }) { 
     @Override 
     protected HashMap<String,String> getParams() { 

      HashMap<String, String> params = new HashMap<String, String>(); 
     params.put("id", BaseActivity.getBaseActivity().getEstablishment_id()); 
     params.put("type", "photos"); 
     params.put("example_file",file.toString()); 

     } 


    }; 
+0

上傳圖像使用Cloudinary第三方庫。它是一個CDN,您可以在其中保存圖像並以不同分辨率從中檢索圖像。 – Pankaj

+0

似乎你是發送圖像作爲一個文件。你需要使用_MultiPartEntity_和抽籤。 – Piyush

+0

'params.put(「example_file」,file.toString()'。如果你在上傳文件時遇到問題,那麼你應該準確地顯示你在'file'中的內容是什麼以及如何放入內容。 – greenapps

回答

0
private void uploadImageWithStringParamsToServer() { 
    SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST, "http://139.59.16.103/addImage", 
     new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       Log.d("Response", response); 

       Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show(); 

      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
    }); 

    smr.addStringParam("id", "821470"); 
    smr.addStringParam("type", "photos"); 
    smr.addFile("example_file", "2014-11-17 17.08.33-2.jpg"); 
    smr.setFixedStreamingMode(true); 
    smr.setOnProgressListener(this); 

    RequestQueue mRequestQueue = Volley.newRequestQueue(this); 
    mRequestQueue.add(smr); 
    mRequestQueue.start(); 
} 
+0

你可以發佈SimpleMultiPartRequest的代碼嗎? –

+0

看https:// github.com/DWorkS/VolleyPlus/blob/master/library/src/com/android/volley/request/SimpleMultiPartRequest.java @AnishKumar –

+0

Thanks @Clive Seebregts –

相關問題