Android 通过Http访问Web端的Servlet
/////Http工具类
import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; public class HttpUtil { public static String getHttpJSON(String url) { // String httpUrl = // "http://www.weixiweb.cn/memberOrder.do?op=memberOrderJson"; HttpGet httpRequest = new HttpGet(url); try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse httpResponse = httpclient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String jsonStr = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); return jsonStr; } } catch (Exception e) { e.printStackTrace(); System.out.println("==============e.printStackTrace() : " + e.getMessage()); } return null; } public static int getHttpStatus() { int status = 0; HttpGet httpRequest = new HttpGet( "http://192.168.0.214/vote/AndroidConnServlet"); try { ///生成HttpParams对象,设置网络连接的时间 HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); HttpConnectionParams.setSoTimeout(httpParameters, 5000); HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpClient httpclient = new DefaultHttpClient(httpParameters); // HttpParams params = httpclient.getParams(); HttpResponse httpResponse = httpclient.execute(httpRequest); // HttpConnectionParams.setConnectionTimeout(params, 3000); // HttpConnectionParams.setSoTimeout(params, 5000); status = httpResponse.getStatusLine().getStatusCode(); } catch (Exception e) { e.printStackTrace(); System.out .println("==============connection wifi fail,e.printStackTrace() : " + e.getMessage()); } return status; } }
////调用方法
public void ensureVote() { String URL = "http://192.168.0.214/vote/VoteServlet"; codeText = codeEdit.getText().toString(); if (codeText == null || codeText.length() == 0) { Toast.makeText(VoteActivity.this, "投票失败,请输入投票码.", Toast.LENGTH_LONG).show(); return; } URL = URL + "?project=" + radioVoteText + "&voteCode=" + codeText + "&source=Android"; String httpStatus = "0"; httpStatus = HttpUtil.getHttpJSON(URL); if (httpStatus != null && httpStatus.equals("1")) { new AlertDialog.Builder(VoteActivity.this).setTitle("success") .setMessage("投票成功,非常感谢.").setNeutralButton("返回", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { } }).show(); } else if (httpStatus != null && httpStatus.equals("2")) { new AlertDialog.Builder(VoteActivity.this).setTitle("warn") .setMessage("投票失败,投票码已经使用.").setNeutralButton("返回", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { } }).show(); } else if (httpStatus != null && httpStatus.equals("0")) { new AlertDialog.Builder(VoteActivity.this).setTitle("error") .setMessage("投票失败,请联系网管.").setNeutralButton("返回", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { } }).show(); } } }
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01