| Thành viên | Trả lời |
radiogaga
 Ta đây thủy tinh, có tài gọi mưa hô gió ...
Sơn Tinh ta đây : có tài là đốn gốc cây, đánh hốc cây ... 45 bài
| 25-7-2019 9:17:38 package com.dhag.securestamp;
import android.os.AsyncTask; import android.util.Log; import java.util.ArrayList;
public class ApiMultipart extends AsyncTask { public ApiMultipart() {}
private String file_name = "sample.jpg"; private byte[] fileContent; private ArrayList collections = new ArrayList(); private SomeEventListener listener;
public void setFileData(String file_name, byte[] inputData) { this.file_name = file_name; this.fileContent = inputData; }
public void addFormData(String name, String value) { NameValue nv = new NameValue(name, value); collections.add(nv); }
public void setSomeEventListener (SomeEventListener listener) { this.listener = listener; }
@Override protected String doInBackground(String... params) { String baseUrl = params[0]; String data = ""; try { HttpClient client = new HttpClient(baseUrl); client.connectForMultipart(); for(int i =0; i < collections.size(); i++) { NameValue obj = (NameValue)collections.get(i); client.addFormPart(obj.getName(), obj.getValue()); }
client.addFilePart("fileUp", file_name, fileContent); client.finishMultipart();
data = client.getResponse(); } catch(Exception ex) { ex.printStackTrace(); } return data; }
@Override protected void onPostExecute(String data) { if (listener != null) listener.onCompleteEvent (); }
}
============ package com.dhag.securestamp;
import android.os.AsyncTask; import android.util.Log;
import org.json.JSONException; import org.json.JSONObject;
import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL;
public class ApiPost extends AsyncTask { public String msg; private SomeEventListener listener; private JSONObject jsonObject = null; private String http_method = "POST";
public ApiPost() {}
public void setJsonObj(JSONObject myObject){ jsonObject = myObject; }
public void setMethod(String method) { http_method = method; }
public void setSomeEventListener (SomeEventListener listener) { this.listener = listener; }
@Override protected String doInBackground(String... urls) { // params comes from the execute() call: params[0] is the url. try { try { return HttpPost(urls[0]); } catch (JSONException e) { e.printStackTrace(); return "Error!"; } } catch (IOException e) { return "Unable to retrieve web page. URL may be invalid."; } }
private String HttpPost(String myUrl) throws IOException, JSONException { URL url = new URL(myUrl);
// 1. create HttpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod(http_method); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
// 2. build JSON object //JSONObject jsonObject = buidJsonObject();
// 3. add JSON content to POST request body if(jsonObject != null) setPostRequestContent(conn, jsonObject);
// 4. make POST request to the given URL conn.connect();
// 5. get return json InputStream inStream = new BufferedInputStream(conn.getInputStream()); ByteArrayOutputStream into = new ByteArrayOutputStream(); byte[] buf = new byte[4096]; for (int n; 0 < (n = inStream.read(buf));) { into.write(buf, 0, n); } into.close(); msg = new String(into.toByteArray(), "UTF-8");
// 6. return response message return conn.getResponseMessage() + ""; }
private void setPostRequestContent(HttpURLConnection conn, JSONObject jsonObject) throws IOException { OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(jsonObject.toString()); Log.i("ApiPost", jsonObject.toString()); writer.flush(); writer.close(); os.close(); }
@Override protected void onPostExecute(String result) {
if (listener != null) listener.onCompleteEvent (); } }
|
 |