0
Görüntüyü yüklemek için HttpURLConnection kullanarak sunucuya json isteği göndermem gerekir. HttpClient ile çalışıyor. ama bir istek yapmak istiyorum HttpURLConnection ve MultipartEntity.any bir yardım me .. İşte HttpURLConnection ile sunucuya bir istek göndermek için JSON parametrelerini kullanarak
HttpClient ait kodudur: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;
} }
ben sadece .. değil Volley kütüphanesi ile HttpUrlConnection ile göndermek gerekir bu bağlantıyı görebilirsiniz – sree
Bu bağlantıyı ... http bakın a-web hizmeti kullanan-httpurlconnection –