在公司一个项目中用到了这样一种网络请求方式,在此记录学习。
1.java代码
/**
* 访问网络改变座位号
*/
private void saveSeatNumber() {
Controller.getInstance(this).saveSeatNumber(
getIntent().getStringExtra("prescriptionGroupNum"),
seatNumberContent.getText().toString().trim(),
new HandlerMessage() {
@Override
public void handlerMessage(String result) {
if (result.equals("1")) {
} else if (result.equals("2")) {
Toast.makeText(InfusionSeatNumberActivity.this,
"保存失败", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(InfusionSeatNumberActivity.this,
"未知错误", Toast.LENGTH_LONG).show();
}
}
});
}
2.Controller.java
public class Controller {
private MySQLiteOpenHelper myOpenHelper;
private AsyncTask task;
private static Controller instance;
private static Context context;
public static Controller getInstance(Context c) {
if (instance == null) {
instance = new Controller();
}
context = c;
return instance;
}
public void cancelTask(boolean cancel) {
if (task != null) {
task.cancel(cancel);
}
}
/**
* 保存座位号
*
* @param prescriptionGroupNum
* @param zw
*/
public void saveSeatNumber(final String prescriptionGroupNum,
final String zw, final HandlerMessage handlerMsg) {
AsyncTask<Object, Integer, String> asyncTask = new AsyncTask<Object, Integer, String>() {
ProgressDialog pd;
@Override
protected void onPreExecute() {
pd = new ProgressDialog(context,
ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage(context.getString(R.string.pleasewait));
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(Object... objects) {
return ServerDataUtil.getInstance().saveSeatNumber(
prescriptionGroupNum, zw);
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
handlerMsg.handlerMessage(result);
}
pd.dismiss();
}
};
asyncTask.execute("");
}
}
3.ServerDataUtil.java
public class ServerDataUtil {
private final static int TIMEOUT = 30000;
public final static String CONEXCEPTION = "网络异常,请重新操作";
private HttpPost req;
private HttpResponse rep;
private static DefaultHttpClient client;
private List<NameValuePair> params;
private static ServerDataUtil fetcher;
public static ServerDataUtil getInstance() {
if (fetcher == null) {
fetcher = new ServerDataUtil();
}
if (client == null) {
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory
.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(
params, schemeRegistry);
client = new DefaultHttpClient(cm, params);
final HttpParams param = client.getParams();
HttpConnectionParams.setConnectionTimeout(param, TIMEOUT);
HttpConnectionParams.setSoTimeout(param, TIMEOUT);
ConnManagerParams.setTimeout(param, TIMEOUT);
}
return fetcher;
}
/**
* 保存座位号
*
* @return
*/
public String saveSeatNumber(String prescriptionGroupNum, String zw) {
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("prescriptionGroupNum",
prescriptionGroupNum));
params.add(new BasicNameValuePair("zw", zw));
try {
req = new HttpPost(ClientStrings.getBASE_URL()
+ ClientStrings.SAVE_SEAT_NUMBER);
HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
req.setHeader(entity.getContentType());
req.setEntity(entity);
rep = client.execute(req);
if (rep.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(rep.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return Static.netexception;
}
4.HandlerMessage.java
/**
* 异步返回值
*
* @author Administrator
*
*/
public interface HandlerMessage {
public void handlerMessage(String result);
}