【发布时间】:2016-04-04 06:28:38
【问题描述】:
我需要使用 HttpURLConnection 将 json 请求发送到服务器以上传图像。使用 HttpClient 可以正常工作。但我想用 HttpURLConnection 和 MultipartEntity 提出请求。任何人都可以帮助我..
这里是HttpClient的代码:
public class MultiPartRequest extends AsyncTask<Object, Integer, JSONObject> {
private AsyncHttpCallback listener;
private final String TAG = "HTTP_MULTIPART";
private Activity activity;
public MultiPartRequest(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(Object... inputParams) {
String requestUrl = (String) inputParams[0];
JSONObject multipartParams = (JSONObject) inputParams[1];
HashMap<String, Object> requestObject = (HashMap<String, Object>) inputParams[2];
HttpClient httpClient = new DefaultHttpClient();
JSONObject finalResult = new JSONObject();
try {
finalResult = new JSONObject("{\"error\":true,\"message\":\"Something went wrong\"}");
HttpPost httpPost = new HttpPost(requestUrl);
StringEntity entity = new StringEntity(multipartParams.toString());
entity.setContentType("application/json");
httpPost.setEntity(entity);
// httpPost.setParams(multipartParams);
MultipartEntity multipartEntity = new MultipartEntity();
for (Entry<String, Object> obj : requestObject.entrySet()) {
String fileName = obj.getKey();
Log.d("Multipart", fileName);
if (fileName.equalsIgnoreCase("data")) {
Log.d("objectdata", obj.getValue().toString());
multipartEntity.addPart(fileName, (StringBody) obj.getValue());
} else {
multipartEntity.addPart(fileName, (FileBody) obj.getValue());
}
}
httpPost.setEntity(multipartEntity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null; ) {
builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
finalResult = new JSONObject(tokener);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return finalResult;
}
@Override
public void onPostExecute(JSONObject result) {
Log.d(TAG, "The response object is: " + result.toString());
try {
String errorMsg = JSONHandler.getStringFromJSONObject(result, "message");
if (errorMsg.equalsIgnoreCase("Authentication problem with the token provided") || JSONHandler.getIntFromJSONObject(result, "code") == 5100) {
MyApplication.displayToast(errorMsg);
Intent intent = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivity(intent);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
if (result.has("error")) {
listener.errorCallback(result);
} else {
listener.successCallback(result);
}
}
public void setOnResultsListener(AsyncHttpCallback listener) {
this.listener = listener;
} }
【问题讨论】:
标签: android json web-services android-activity httpurlconnection