0

为了上传图片的目的,我使用android volley库向服务器发送多部分请求。我已经为这个库编写了一些自定义代码。 HtppEntity在这里用作这个文件中的一个类,但是现在我收到了一个警告,指出HttpEntity已被弃用。我碰巧知道HttpurlConnection是一种替代方案,但我不知道如何将其替换为我的代码?HttpEntity在android中替代吗?

这里是我的代码

import com.android.volley.AuthFailureError; 
import com.android.volley.NetworkResponse; 
import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.VolleyLog; 

import org.apache.http.HttpEntity; 
import org.apache.http.entity.ContentType; 
import org.apache.http.entity.mime.HttpMultipartMode; 
import org.apache.http.entity.mime.MultipartEntityBuilder; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.util.CharsetUtils; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.Map; 

/** 
* Created by JoeyJAL on 2015/3/14. 
*/ 
public class MultiPartRequest extends Request<String> { 

    MultipartEntityBuilder entity = MultipartEntityBuilder.create(); 
    HttpEntity httpentity; 
    private String FILE_PART_NAME = "imageFile"; 

    private final Response.Listener<String> mListener; 
    private final File mFilePart; 
    private final Map<String, String> mStringPart; 

    public MultiPartRequest(String url, Response.ErrorListener errorListener, 
          Response.Listener<String> listener, File file, 
          Map<String, String> mStringPart) { 
     super(Method.POST, url, errorListener); 

     this.mListener = listener; 
     this.mFilePart = file; 
     this.mStringPart = mStringPart; 

     entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     try { 
      entity.setCharset(CharsetUtils.get("UTF-8")); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     buildMultipartEntity(); 
     httpentity = entity.build(); 
    } 

    private void buildMultipartEntity() { 
     entity.addPart(FILE_PART_NAME, new FileBody(mFilePart, ContentType.create("image/jpeg"), mFilePart.getName())); 
     if (mStringPart != null) { 
      for (Map.Entry<String, String> entry : mStringPart.entrySet()) { 
       entity.addTextBody(entry.getKey(), entry.getValue()); 
      } 
     } 
    } 

    @Override 
    public String getBodyContentType() { 
     return httpentity.getContentType().getValue(); 
    } 

    @Override 
    public byte[] getBody() throws AuthFailureError { 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     try 
     { 
      httpentity.writeTo(bos); 
     } 
     catch (IOException e) 
     { 
      VolleyLog.e("IOException writing to ByteArrayOutputStream"); 
     } 
     return bos.toByteArray(); 
    } 

    @Override 
    protected Response<String> parseNetworkResponse(NetworkResponse response) { 

     try { 
      System.out.println("Network Response "+ new String(response.data, "UTF-8")); 
      return Response.success(new String(response.data, "UTF-8"), 
        getCacheEntry()); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
      return Response.success(new String(response.data), getCacheEntry()); 
     } 
    } 

    @Override 
    protected void deliverResponse(String response) { 
     mListener.onResponse(response); 
    } 
} 
+0

欢迎来到StackOverflow。这不是其他人将免费接管你的工作的地方。首先,你需要证明你尝试了一些东西,并与我们分享。 –

+0

@Rahul你只能得到你的问题和问题的答案/解决方案。没有人会在这里做你的工作。 –

+0

我不会告诉你我的工作只是要求更换code.I无法理解,@ kibzorg –

回答

0
URL url = new URL("http://yoururl.com"); 
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
conn.setReadTimeout(10000); 
conn.setConnectTimeout(15000); 
conn.setRequestMethod("POST"); 
conn.setDoInput(true); 
conn.setDoOutput(true); 

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
params.add(new BasicNameValuePair("firstParam", paramValue1)); 
params.add(new BasicNameValuePair("secondParam", paramValue2)); 
params.add(new BasicNameValuePair("thirdParam", paramValue3)); 

OutputStream os = conn.getOutputStream(); 
BufferedWriter writer = new BufferedWriter(
     new OutputStreamWriter(os, "UTF-8")); 
writer.write(getQuery(params)); 
writer.flush(); 
writer.close(); 
os.close(); 

conn.connect(); 

getQuery(名单)将有助于产生的输出流。您正在上传图像,以便通过替换getQuery()函数将其直接更改为输出流。

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException 
    { 
     StringBuilder result = new StringBuilder(); 
     boolean first = true; 

     for (NameValuePair pair : params) 
     { 
      if (first) 
       first = false; 
      else 
       result.append("&"); 

      result.append(URLEncoder.encode(pair.getName(), "UTF-8")); 
      result.append("="); 
      result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); 
     } 

     return result.toString(); 
    } 
+0

与m问题无关。@ kibzong –