1

我的問題是與writeArgsToConn()函數.....我想。我不知道如何實現它。通過Android發佈多部分表單數據AsyncTask

我想從Android設備使用AsyncTask類發佈multipart-formdata。有人能幫忙嗎?我想遠離折舊的org.apache.http.legacy內容並堅持使用最新的Android庫。

我能夠對類DoPostJSON使用Content-Type:application/json的類使用類似的實現,並且該類可以正常工作。

同樣的問題,但在Reddit上:https://redd.it/49qqyq

我曾與獲得表達的NodeJS服務器來檢測所發送的參數問題我DoPostJSON類工作得很好,我的服務器的NodeJS能夠檢測參數......一些。理由DoPostMultiPart不工作和nodejs服務器不能看到傳入參數。我覺得我正在使用錯誤的方式庫。

public class DoPostMultiPart extends AsyncTask<JSONObject, Void, JSONObject> implements Post{ 

    @Override 
    public HttpURLConnection createConn(String action) throws Exception{ 
     URL url = new URL(Utils.host_api + action); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("POST"); 

     conn.setDoInput(true); 
     conn.setDoOutput(true); 

     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.setRequestProperty("Cache-Control", "no-cache"); 

     conn.setReadTimeout(35000); 
     conn.setConnectTimeout(35000); 
     return conn; 
    } 

    @Override 
    public JSONObject getResponse(HttpURLConnection conn) throws Exception { 
     int responseCode = conn.getResponseCode(); 
     String response = ""; 
     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(in)); 
      String line = ""; 
      StringBuilder stringBuilder = new StringBuilder(); 
      while ((line = responseStreamReader.readLine()) != null) 
       stringBuilder.append(line).append("\n"); 
      responseStreamReader.close(); 
      response = stringBuilder.toString(); 
     } else { 
      throw new Exception("response code: " + responseCode); 
     } 
     conn.disconnect(); 
     return new JSONObject(response); 
    } 

    // TODO: fix this function 
    @Override 
    public void writeArgsToConn(JSONObject args, HttpURLConnection conn) throws Exception { 
     // define paramaters 
     String fullname = args.getString("fullname"); 
     String email = args.getString("email"); 
     String password = args.getString("password"); 
     String confpassword = args.getString("confpassword"); 
     Bitmap pic = (Bitmap) args.get("pic"); 

     // plugin paramters into request 
     OutputStream os = conn.getOutputStream(); 
     // how do I plugin the String paramters??? 
     pic.compress(Bitmap.CompressFormat.JPEG, 100, os); // is this right??? 
     os.flush(); 
     os.close(); 
    } 

    @Override 
    protected JSONObject doInBackground(JSONObject... params) { 
     JSONObject args = params[0]; 
     try { 
      String action = args.getString("action"); 
      HttpURLConnection conn = createConn(action); 
      writeArgsToConn(args, conn); 
      return getResponse(conn); 
     } catch (Exception e) { 
      Utils.logStackTrace(e); 
      return null; 
     } 
    } 
} 
+0

使用一些http庫來幫助你。這是浪費時間編寫自定義json http的東西。 – zapl

+0

[Android的POST多部分表單數據]可能的重複(http://stackoverflow.com/questions/7645284/android-post-multipart-form-data) – Samuel

+0

是的,我對Android http請求的東西沒有經驗。我想知道人們是否可以指出我去哪個圖書館的正確方向。不太確定那裏有什麼。 – IndoNinja

回答

0

我使用OkHttpClient庫解決了我的問題。

JSONObject args = params[0]; 
try 
{ 
    final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); 

    RequestBody requestBody = new MultipartBuilder() 
    .type(MultipartBuilder.FORM) 
    .addFormDataPart("fullname", args.getString("fullname")) 
    .addFormDataPart("email", args.getString("email")) 
    .addFormDataPart("password", args.getString("password")) 
    .addFormDataPart("confpassword", args.getString("confpassword")) 
    .addFormDataPart("pic", "profile.png", RequestBody.create(MEDIA_TYPE_PNG, (File) args.get("pic"))) 
    .build(); 

    Request request = new Request.Builder() 
    .url(Utils.host_api + args.getString("action")) 
    .post(requestBody) 
    .build(); 

    OkHttpClient client = new OkHttpClient(); 
    Response response = client.newCall(request).execute(); 
    return new JSONObject(response.body().string()); 
} 
catch (Exception e) 
{ 
    Utils.logStackTrace(e); 
    return null; 
} 
相關問題