写一个类继承AsyncHttpResponseHandler ,如:
class HttpResponse extends AsyncHttpResponseHandler {
private int type;
private RequestParams params;
public HttpResponse(int type, RequestParams params) {
this.type = type;
this.params = params;
}
@Override
public void onStart() {
// Initiated the request
Log.v("post", "upload In Turn on start...");
}
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// Successfully got a response
try {
String result = new String(responseBody, "utf-8");
Log.v("post", "on Success请求成功结果为:" + result);
JSONObject resultJson = new JSONObject(result);
String code = resultJson.getString("status_code");
if ("0".equals(code)) {
// 上传成功
Log.v("post", "success-------------");
Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_SHORT)
.show();
//上传成功后需要跳转的页面
Intent intent = new Intent(RegActivity.this,
LoginActivity.class);
startActivity(intent);
} else {
Log.v("post", "fail------------");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// Response failed :(
}
private void doUploadFailure(String error) {
}
@Override
public void onRetry() {
// Request was retried
}
@Override
public void onProgress(int bytesWritten, int totalSize) {
// Progress notification
Log.v("post", "on Progress上传进度:" + (int) (bytesWritten * 100 / totalSize) + "%");
load_prgress.setVisibility(View.VISIBLE);
}
@Override
public void onFinish() {
// Completed the request (either success or failure)
Log.v("post", "on Finish上传完成!");
load_prgress.setVisibility(View.GONE);
}
}
playframework服务器提供的接口如:http://127.0.0.1/webservice/registerUser
返回值:json
请求方式:/webservice/registerUser
参数列表:
输入:
参数 |
类型 |
说明 |
|
userName |
long |
用户名 |
非空 |
userPwd |
String |
用户密码 |
非空 |
confirmPwd |
String |
确认密码 |
非空 |
uploadFile |
File |
头像 |
|
输出结果:
节点 |
类型 |
说明 |
status_code |
int |
状态码,0为成功,1为失败 |
msg |
String |
返回说明(包含失败时的信息) |
result[user] |
String |
用户信息 |
构造上传的参数:
public static RequestParams getRegParams(String userName, String userPwd,String confirmPwd, String uploadFile) throws FileNotFoundException{
RequestParams params = new RequestParams();
params.put("userName", userName);
params.put("userPwd", userPwd);
params.put("confirmPwd", confirmPwd);
if(uploadFile != null && !uploadFile.isEmpty()){
File file = new File(uploadFile);
InputStream is = new FileInputStream(file);
params.put("uploadFile", is, file.getName());
}else{
// params.put("uploadFile", null);
}
return params;
}
客户端通过接口上传图片:
RequestParams params;
try {
params = PrefUtil.getRegParams(UserName, PassWord, ConfirmPassWord, picPath);
AsyncHttpClient client = new AsyncHttpClient();
client.post(Const.requestURL, params, new HttpResponse(0, params));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
playframework中后台的代码:
/**
* 注册普通用户
* @param userName
* @param userPwd
* @param confirmPwd
* @throws IOException
*/
public static void registerUser(String userName, String userPwd, String confirmPwd, java.io.File uploadFile) throws IOException{
if(userName.isEmpty() || userPwd.isEmpty() || confirmPwd.isEmpty()){
status_code = Const.FAIL;
msg = Const.LOGIN_ERROR_EMPTY;
}else{
System.out.println("upload user photo");
VUser user = new VUser(userName, userPwd, confirmPwd);
user.checked = Const.CHECKED;
user.role = Const.USER;
user.save();
String fileName = user.userName + "_" + uploadFile.getName();
writePhoto(uploadFile, fileName);
VUser u = VUser.findById(user.getId());
u.photoPath = fileName;
u.save();
status_code = Const.SUCCESS;
msg = "";
dataMap.put("result", VUser.singleUserBean(user));
}
dataMap.put("status_code", status_code);
dataMap.put("msg", msg);
renderJSON(dataMap);
}
public static String writePhoto(java.io.File pFile, String fileName) throws IOException{
// java.io.File pFile = new java.io.File(Blob.getStore(), "xiaohong_lyc.jpg");
BufferedImage image = ImageIO.read(pFile);
//要想保存这个对象的话你要把image声明为BufferedImage 类型
// String fileName = user.userName + "_" + pFile.getName();
String type = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
java.io.File fo = new java.io.File(Blob.getStore(), fileName); // 将要转换出的小图文件
ImageIO.write(image, type, fo);
return "";
}