我可以使用這種方式使用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的 –