我可以使用这种方式使用Okhttp
将字符串上传到服务器吗?使用okhttp发送数据而没有异步任务
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
usernam = (EditText) findViewById(R.id.username);
passw = (EditText) findViewById(R.id.password);
email = (EditText) findViewById(R.id.email);
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
}
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name",name);
jsonObject.accumulate("password",pass);
jsonObject.accumulate("email",emails);
json = jsonObject.toString();
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
可能包括与反应式编程,像RX-java的 –