0
こんにちは!私はこれに似た要求をしようとしています:AndroidとObjectiveを使用したマルチパートフォームデータ(NSDictionaryとImage) - C
public boolean ChangeAvatar(final Bitmap avatarBitmap, final IChangeAvatar handler)
{
String url = baseUrl + Language.getInstance().getLanguage() + "/my_url";
final JSONObject params = new JSONObject();
try {
JSONObject auth = new JSONObject();
auth.put("uid",Long.toString(UserData.getInstance().getUserId()));
auth.put("token", UserData.getInstance().getUserToken());
params.put("auth",auth);
params.put("act","avatar_change");
}
catch (Exception e)
{
Log.e("network",e.getMessage());
return false;
}
VolleyMultipartRequest myRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
try {
String responseStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
handler.ChangeSuccess("");
//JSONObject resp = new JSONObject(responseStr);
Log.d("network", responseStr);
}
catch (Exception e)
{
handler.ChangeFailed("error","Error");
Log.d("network", e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error.getMessage()!= null)
handler.ChangeFailed("error",error.getMessage());
else
{
try {
String responseStr = new String(error.networkResponse.data, HttpHeaderParser.parseCharset(error.networkResponse.headers));
handler.ChangeFailed("error","errorMessage");
}
catch (Exception e)
{
handler.ChangeFailed("error","errorMessage");
}
}
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String,String> paramsMap = new HashMap<>();
paramsMap.put("json",params.toString());
return paramsMap;
}
@Override
protected Map<String, DataPart> getByteData() {
HashMap<String,DataPart> result = new HashMap<>();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
avatarBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
final byte[] byteArrayImage = baos.toByteArray();
result.put("userfile",new DataPart("avatar.jpg",byteArrayImage,"image/jpeg"));
return result;
}
};
myRequest.setRetryPolicy(new DefaultRetryPolicy(5000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestManager.getInstance(context).addToRequestQueue(myRequest);
return true;
}
私はいつもエラーが発生します。私の問題は自分のイメージのキー転送が間違っているようです。ここ は、現在の開発は、以下のとおりです。
- (IBAction)saveAction:(UIButton *)sender
{
NSMutableDictionary *auth = [NSMutableDictionary dictionary];
[auth setValue:[UserDefaultsHandler getUserDefaultsValueByKey:@"myId"] forKey:@"uid"];
[auth setValue:[UserDefaultsHandler getUserDefaultsValueByKey:@"token"] forKey:@"token"];
NSMutableDictionary *userDataDictionary = [NSMutableDictionary dictionary];
[userDataDictionary setValue:auth forKey:@"auth"];
[userDataDictionary setValue:@"avatar_change" forKey:@"act"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"myUrl"]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSData* imageData = UIImageJPEGRepresentation(self.imageView.image, 1.0f);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; fileName=\"avatar.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter all_data
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"all_data\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *jsonStr = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:userDataDictionary options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
[body appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
NSError *error=nil;
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}