0
Ich muss die JSON-Anfrage an Server senden mit HttpURLConnection, um das Bild hochzuladen. mit HttpClient funktioniert es gut. aber ich möchte einen Antrag machen mit HttpURLConnection und MultipartEntity.any man mir helfen ..So senden Sie eine Anfrage an den Server mit HttpURLConnection mithilfe von JSON-Parametern
Hier ist der Code von 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;
} }
i nur mit HttpURLConnection senden müssen .. nicht mit Volley Bibliothek – sree
Siehe diesen Link ... http: //javatechig.com/snippets/upload-image-to- a-Web-Service-Nutzung-httpurlconnection –