OKHttp上传文件,如何获取返回的结果
1.以前我用volley框架上传文件的时候可以直接获取到返回的结果。
2.现在换了Okhttp之后,都不知道返回的结果在哪里
返回结果:
方法一
try {
Response response = call.execute();
String result = response.body().string();
if(response.isSuccessful()){
listener.onUploadSuccess(result);
}else{
listener.onUploadFail();
}
} catch (IOException e) {
e.printStackTrace();
}
方法二
`call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("upload", "上传失败" + e.toString());
e.printStackTrace();
removeRequest(tag);
listener.onUploadFail();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
Log.e("upload", "上传请求成功" + response.body().string());
listener.onUploadSuccess(response.body().string());
removeRequest(tag);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
});
`
以上方法都不行~求大神解救
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
volley 你是怎么上传的,求解答
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.Response.ErrorListener;
import com.android.volley.toolbox.HttpHeaderParser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
public class UploadFileRequest extends Request<String> {
Creates a new request with the given url.
*
@param url URL to fetch the string at
@param listener Listener to receive the String response
*/
UploadFileRequest(String url, Listener<String> listener) {
}
/**
Creates a new POST request.
*
@param url URL to fetch the string at
@param listener Listener to receive the String response
@param errorListener Error listener, or null to ignore errors
*/
UploadFileRequest(String url, Listener<String> listener, ErrorListener errorListener) {
}
/**
@return
*/
UploadFileEntity getMultiPartEntity() {
}
@Override
String getBodyContentType() {
}
void addHeader(String key, String value) {
}
@Override
Map<String, String> getHeaders() throws AuthFailureError {
}
@Override
byte[] getBody() {
}
@Override
Response<String> parseNetworkResponse(NetworkResponse response) {
}
@Override
void deliverResponse(String response) {
}
}
/**
@param account
@param bitmap
@param webUrl
@param uploadListener
*/
static void uploadImage(String account, Bitmap bitmap, String webUrl, final UploadListener uploadListener) {