Web后台和Android前端之间的网络通信
来源:互联网 发布:装潢预算软件 编辑:程序博客网 时间:2024/05/01 22:07
一、通过GET方式去提交数据:
public void loginGet(View view){//例如: String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); String path="http://193.168.2.141:7788/demo/login.do"; new MyGetTask().execute(uname,upass,path); }
class MyGetTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { String uname=params[0].toString(); String upass=params[1].toString(); String path=params[2].toString(); try { URL url=new URL(path+"?uname="+uname+"&upass="+upass); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); if(connection.getResponseCode()==200){ InputStream is=connection.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String s=br.readLine(); return s; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); String s= (String) o; Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } }
二、通过POST提交数据
public void loginPost(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); String path="http://193.168.2.141:7788/demo/login.do"; new MyPostTask().execute(uname,upass,path); }
class MyPostTask extends AsyncTask{ @Override protected Object doInBackground(Object[] params) { String uname=params[0].toString(); String upass=params[1].toString(); String path=params[2].toString(); try { URL url=new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); //admin 123 //uname=admin&upass=123456 String s="uname="+uname+"&upass="+upass; //添加请求头 conn.setRequestProperty("Content-Length",s.length()+""); conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//固定的写法 conn.setDoOutput(true);//允许对外输出数据 OutputStream os=conn.getOutputStream(); os.write(s.getBytes()); if(conn.getResponseCode()==200){ InputStream is=conn.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String str=br.readLine(); return str; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); String s= (String) o; Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show(); } }
三、使用第三方AsyncHttpClient提交数据
public void loginAsyncHttpClient(View view){ String uname=et_main_uname.getText().toString(); String upass=et_main_upass.getText().toString(); String path="http://193.168.2.141:7788/G160628_Server/login.do"; AsyncHttpClient ahc=new AsyncHttpClient(); RequestParams params=new RequestParams(); params.put("uname",uname); params.put("upass",upass); ahc.post(this,path,params,new TextHttpResponseHandler(){ @Override public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) { super.onFailure(statusCode, headers, responseBody, error); } @Override public void onSuccess(int statusCode, Header[] headers, String responseBody) { super.onSuccess(statusCode, headers, responseBody); Toast.makeText(MainActivity.this, responseBody, Toast.LENGTH_SHORT).show(); } }); }
阅读全文
0 0
- Web后台和Android前端之间的网络通信
- 前端、后台和连接前端后台的网络数据传输
- Java后台与js前端之间的通信问题
- Android模拟器之间的网络通信
- Android前台画面和后台service之间通信的方法之Broadcast
- Android前台画面和后台service之间通信的方法之Broadcast
- Android前台画面和后台service之间通信的方法之Broadcast(转)
- Android前台画面和后台service之间通信的方法之Broadcast
- Android前台画面和后台service之间通信的方法之Broadcast
- Socket通信:实现PC端和Android端之间的相互通信(通过网络)
- 网页设计、web前端、后台的开发流程和注意事项
- 网页设计、web前端、后台的开发流程和注意事项
- Android Activity和Service之间的通信
- Android Activity和Service之间的通信
- Android Activity和Service之间的通信
- Android Activity和Service之间的通信
- PHP和Android之间的通信
- web前台和后台之间对象的传递
- 判断一个树是否为完全二叉树
- c++适配器
- hdu2049 不容易系列之(4)——考新郎(C语言)
- Sherlock and his girlfriend
- Eclipse-deploy path部署路径
- Web后台和Android前端之间的网络通信
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Ca
- 基于Spring+JMX+Tomcat实现资源动态管理
- Mongodb从0到1系列四: Limit与Skip方法、排序、索引
- lower_bound,upper_bound的用法
- oracle---geometry类型转换
- 203. Remove Linked List Elements
- html5使用canvas画布制作圆形加载动画
- (二分, 字符串)Codeforces Round #402 D. String Game