Android中与服务器通信
来源:互联网 发布:吴用 知乎 编辑:程序博客网 时间:2024/06/06 00:21
服务器开发语言我选择的是php
服务器搭建完成之后,在服务器www目录下写一个demo.php,php的代码如下:
<?php
if($_POST['username'] == "admin"){
echo "admin";
}else{
echo "user";
}
?>
然后在安卓中向该服务器发送请求:
public class http extends AppCompatActivity { HttpUtil httpUtil; TextView textView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http); httpUtil = new HttpUtil(); Button button = (Button)findViewById(R.id.send); textView = (TextView)findViewById(R.id.text3); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String str = httpUtil.sendHttpRequest("http://115.159.217.226/demo.php"); textView.setText(str); } }); }} 先写一个http类,调用我自己写的sendHttpRequest();方法 方法的代码如下:public class HttpUtil { private OkHttpClient client; private Request request; private Response response; public String str; public String sendHttpRequest(final String address){ new Thread(new Runnable() { String responseData; public void run() { try{ client = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("username","admin") .build(); request = new Request.Builder() .url(address) .post(requestBody) .build(); response = client.newCall(request).execute(); responseData = response.body().string(); str = responseData; }catch (Exception e){ e.printStackTrace(); } } }).start(); return str; }}最后得到数据为admin,向php服务器通信成功
阅读全文
0 0
- Android中与服务器通信
- Android中使用Http与服务器进行通信
- android与服务器servlet通信
- Android与Mysql服务器通信
- Android与服务器Socket通信
- Android与服务器的通信
- Android与服务器通信之socket通信
- Android与服务器通信之socket通信
- Android客户端通过socket与服务器通信
- Android 用Https协议与服务器通信
- Android操作HTTP实现与服务器通信
- 毕业设计之 android客户端与服务器通信
- Android操作HTTP实现与服务器通信
- Android操作HTTP实现与服务器通信
- Android操作HTTP实现与服务器通信
- Android操作HTTP实现与服务器通信
- Android操作HTTP实现与服务器通信
- Android操作HTTP实现与服务器通信
- vue父组件点击触发子组件事件
- Xcode8权限问题
- Web漏洞挖掘
- 枚举的位运算
- SpringMVC
- Android中与服务器通信
- C# 单例模式
- 用到的C++语法
- eclipse导出可直接运行的jar项目
- Destroy 和 DestroyImmediate 使用区别
- linux常用命令sort,uniq,cut,wc
- 552. Student Attendance Record II
- CSS三种样式
- model quantization