Danh sách thành viên | Cá nhân | Nhà đất, bất động sản
Diễn đàn
Cổng thông tin , Chính phủ điện tử, VSP (VerySmart Portal), MVC Model View Controller, json, webapi
Java android api làm việc với API server Thành viên | Nội dung | 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 ... 44 bài
| 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 (); } }
| |
Chủ đề gần đây :
Cùng loại :
Tên file
|
Người đăng
|
Ngày
|
Lượt
|
vspforum.zip
Ma nguon vspforum ngay xua
|
aspnet |
4/18/2023 6:38:37 AM |
8 |
pdfjs.rar
pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone
|
aspnet |
6/21/2022 11:52:48 AM |
2 |
pdfjs2.rar
Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây
|
aspnet |
6/21/2022 11:52:04 AM |
2 |
runner.zip
using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy
|
aspnet |
12/5/2019 5:55:14 PM |
0 |
gmap.zip
google map + marker
|
aspnet |
7/17/2019 2:25:05 PM |
1 |
vinsmarthomeservice.zip
java post json to api, use AsyncTask, event listener
|
aspnet |
7/9/2019 5:00:10 PM |
1 |
fblogin.zip
Login facebook bang javascript SDK
|
aspnet |
7/9/2019 9:16:37 AM |
0 |
autocomplete-location.zip
autocomplete location geo from google place, html + js
|
aspnet |
7/4/2019 4:37:55 PM |
2 |
WebAPI.zip
api for android access db (v1.0.0)
|
aspnet |
7/4/2019 9:14:17 AM |
8 |
KydientuPdf.zip
Ky dien tu file PDF su dung itextsharp
|
aspnet |
4/9/2019 3:30:37 PM |
9 |
GooglePlusLogin.zip
Login Google Plus account, C#, web asp.net ver2.0. Simple connect google APIs. Send key, get token, get full account info
|
aspnet |
6/1/2018 10:41:12 AM |
11 |
WebApplication1.rar
Sample su dung thuat toan ma hoa tripDES, co khoa bi mat (privateKey)
|
aspnet |
3/30/2018 10:06:35 PM |
8 |
NETMdbToolsTestApp.rar
dotNet MdbTools for Access 2003/2007/2016 without Microsoft Jet Engine, source C#, https://www.codeproject.com/Articles/283626/MsAccess-MdbTools-with-MFC-and-NET
|
aspnet |
3/26/2018 11:43:16 PM |
1 |
Cryptography_MD5_TriDES_src.zip
Thuật toán mã hóa 2 chiều TriDES, gồm Encrypt và Decrypt, aspnet 2.0
|
aspnet |
3/22/2018 11:20:44 AM |
3 |
mvc.rar
sample project MVC on C#
|
aspnet |
3/20/2018 9:25:36 AM |
9 |
EduPortal.rar
Edu portal frame work for VB.NET
|
aspnet |
3/14/2018 12:00:41 AM |
13 |
AutoEntity.rar
Gencode vb.net visual studio 2015. dotnet v2.0
|
aspnet |
3/13/2018 11:59:16 PM |
2 |
GenCode.rar
Gencode XML, XSLT, Info, DAL .. engine enterprise for quick app database
|
aspnet |
2/5/2018 9:37:28 AM |
9 |
DataXml.rar
Read DB from SQL to XML file, Convert string TCVN to Unicode
|
aspnet |
1/29/2018 2:15:45 PM |
4 |
DesktopModules.rar
Module quản lý tin tức, CMS, quản lý nhóm tin trên dotnetnuke 6.x
|
aspnet |
3/7/2013 4:47:49 PM |
1715 |
|