如何向服务器发送数据 [英] How to send data to Server

查看:69
本文介绍了如何向服务器发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想登录.那么我如何将用户名和密码发送到我的服务器呢?我为服务器写了一个php脚本.他将收到用户名和密码,如果用户名和密码正确,他将回复成功".那么如何发送到服务器呢?使用 onResponse 发送后?

I want to make a login. So how I send the username and the password to my server? I wrote a php script for the server. He will receive the username and the password and if the username and password are correct he will respond 'succes'. So how to send to the Server? And after sending with onResponse?

推荐答案

您可以使用 OkHttp 轻松实现这一点.为了包含库,请在依赖项中使用以下内容:编译'com.squareup.okhttp3:okhttp:3.2.0'

You can use OkHttp to achieve this easily. In order to include the library use the following in dependencies: compile 'com.squareup.okhttp3:okhttp:3.2.0'

作为示例,您可以像这样使用它:

As a sample you can use it like this:

RequestBody formBody = new FormBody.Builder()
                .add("username", USER_NAME)
                .add("password", PASSWORD)
                .build();
Request request = new Request.Builder()
        .url(url)
        .post(formBody)
        .build();
OkHttpClient client = new OkHttpClient();

Response response = client.newCall(request).execute();
if(response.code == 200){
String responseData = responses.body().string(); 
  //Process the response Data
  }else{
     //Server problem
  }

这篇关于如何向服务器发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆