import android.os.AsyncTask;
import android.util.Log;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by
*/
public class OkHttp extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
//0번째 controller선택 , 1번째 type, 2번째 sendMsg
String url="url대입";
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("type", strings[0])
.add("data",strings[1])
//data는 json으로 보내줘야한다.
.build();
Request request = new Request.Builder()
.url()
.post(body)
// .post(RequestBody.create(MediaType.parse("application/json"), json))
.build();
try{
Response response = client.newCall(request).execute();
Log.i("request : " , request.toString());
Log.i("Response : " ,response.toString());
return response.body().string().trim();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
'Api > RestAPI' 카테고리의 다른 글
Retrofit (0) | 2019.07.18 |
---|---|
QR Code Generator (0) | 2019.05.25 |
Zxing Library (0) | 2019.05.25 |
Okhttp/gson/cloud vision (0) | 2019.05.01 |
Glide 이미지 로딩 (0) | 2019.03.22 |