Từ:
10:32 14/06/2012
Bài:
112
Cảm ơn:
124
Thích:
18
em nghi Cách thứ 2 là sử dụng HTTPPost , user sẽ tiến hành post string lên sv để verify
/**
* HTTP Post , chuyển 1 chuỗi string lên server
*
* @param url
* : đường dẫn
* @param jsonData
* : chuỗi string
* @param callback
* : callback
*/
public static void postJsonDataToUrl(final String url,
final String jsonData, final IOnTransportCallbackData callback,
long timeOut, final Handler handler) {
final AsyncTask<Void, Void, String> asyn = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonData, HTTP.UTF_8);
httppost.setEntity(entity);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entityResponse = response.getEntity();
if (entity != null) {
InputStream instream = entityResponse.getContent();
String result = convertStreamToString(instream);
return result;
}
} catch (Exception ex) {
NTDLogUtils.e(ex.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (callback != null)
callback.onResult(result);
handler.removeCallbacksAndMessages(null);
}
};
asyn.execute();
handler.postDelayed(new Runnable() {
@Override
public void run() {
asyn.cancel(true);
handler.removeCallbacksAndMessages(null);
callback.onResult(null);
}
}, timeOut);
}
public static interface IOnTransportCallbackData {
public void onResult(String object);
}
server kiểm tra json hợp lệ sẽ response lại html