我已经有了一个子类请求
public class AddNewPetRequest extends Request<JSONObject> {
private Response.Listener<JSONObject> listener;
public AddNewPetRequest(String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(Request.Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public AddNewPetRequest(int method, String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
更新问题:我遵循了stackoverflow中的一个答案的模式,我想到了这个实现:
public class MultipartRequest extends Request<String> {
private MultipartEntity entity = new MultipartEntity();
private final Response.Listener<String> mListener;
private HashMap<String, String> mParams;
public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener)
{
super(Method.POST, url, errorListener);
mListener = listener;
buildMultipartEntity();
}
private void buildMultipartEntity()
{
entity.addPart("profile_picture", new FileBody(new File("/storage/emulated/0/Pictures/VSCOCam/2015-07-31 11.55.14 1.jpg")));
try
{
entity.addPart("user_id", new StringBody("15"));
entity.addPart("name", new StringBody("Bogs"));
entity.addPart("gender", new StringBody("Male"));
entity.addPart("date_birth", new StringBody("1999-12-5"));
entity.addPart("breed", new StringBody("monkey"));
}
catch (UnsupportedEncodingException e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
@Override
public String getBodyContentType()
{
return entity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
entity.writeTo(bos);
}
catch (IOException e)
{
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
return Response.success("Uploaded", getCacheEntry());
}
@Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
当我将此请求添加到请求队列时,它以
com响应。Android截击TimeoutError
但是如果检查数据库,则执行请求并向表中添加项目,但是配置文件图片上载只有1字节的大小。另一个问题是,我的数据库项添加了两次。
希望此方法能帮助您:
public int uploadFile(String sourceFileUri, String fileName)
{
String upLoadServerUri = "your_api_url";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
//errMsg=Environment.getExternalStorageDirectory().getAbsolutePath();
if (!sourceFile.isFile())
{
Log.e("uploadFile", "Source File Does not exist");
return 0;
}
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
//conn.setRequestProperty("pid", "4");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode != 200)
{
getActivity().runOnUiThread(new Runnable()
{
public void run() {
Toast.makeText(context, "Il y a une erreur l'hors du trasfert de l'image.", Toast.LENGTH_SHORT).show();
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
// dialog.dismiss();
ex.printStackTrace();
Toast.makeText(context, "MalformedURLException", Toast.LENGTH_SHORT).show();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
Toast.makeText(context,errMsg+ "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
}
// dialog.dismiss();
return serverResponseCode;
}
问题内容: 我有一个图像,我想使用Volley库将该图像上传到我的Web服务,问题是我正在寻找一种方法,但仍然没有找到。 我找到了,但是对我不起作用:http : //develop-for- android.blogspot.com.br/2014/01/using-volley-in-your- application.html 我怎样才能做到这一点 ? 我正在尝试这个。 问题答案: 我对排球
有人知道如何使用com将会话cookie附加到请求吗。Android凌空抽射图书馆?当我登录到一个网站时,它会给我一个会话cookie。浏览器会将该cookie与任何后续请求一起发送回。凌空抽射似乎并没有做到这一点,至少不是自动的。 谢谢
本文向大家介绍Android使用Volley实现上传文件功能,包括了Android使用Volley实现上传文件功能的使用技巧和注意事项,需要的朋友参考一下 一个项目中用到的使用Volley上传头像文件的例子,供大家参考,具体内容如下 调用方法如下: private File mSelectedPictureFile; mSelectedPictureFile是一个File文件,参数名是file 通
本文向大家介绍Android使用Volley框架定制PostUploadRequest上传文件,包括了Android使用Volley框架定制PostUploadRequest上传文件的使用技巧和注意事项,需要的朋友参考一下 发现问题 项目中有发表动态的功能,该功能可以将文本和图片上传至服务器。 Volley通过定制PostUploadRequest实现文件上传的功能,本文以一张图片上传为例。 数据
我正在寻找有关此代码的帮助。我需要补充的是,你可以从这个应用程序上传文件。但我不能让它工作得很好。。。。所以这很有效。我能做的就是下载,而不是上传文件。我能做到吗?我把代码留在下面。请明确具体。考虑只支持Android 3 我的意思是,这是一个网络视图,当你在网站上时,脸谱网点击按钮上传图像,打开一个盒子,让你选择是拍照还是看设备。我想要那个。我能做什么?
我想使用Volley库发出soap post请求。我正在使用以下代码,并得到错误“HTTP/1.1400错误请求”。在上一篇文章中,我使用的Soap库运行良好,但我需要使用Volley库发出请求。我正在使用以下url“http://test.com/TestApp/Services/service.asmx?op=ForgotPassword" 请帮助我用截击发出post soap请求。