0
OKHttp 라이브러리를 사용하는 데 문제가 있습니다. 코드 문제인지 또는 라이브러리가 그런 식으로 작동하는지 확실하지 않지만 여기에 문제가 있습니다. 내 webapi는 다음과 같습니다. FullRest는 ASP.NET WEB API를 사용하여이 서비스를 사용하고 싶습니다. POST 및 GET을 쉽게 할 수있었습니다. 안드로이드가 응답을 적게 받으면 m API에 중단 점을 넣으면 문제가 생깁니다. 그 다음 4 초 그것은 길을 잃는다. 그리고 그것은 좋지 않다. 누군가가 나쁜 연결을 가지고 있고 안드로이드가 사용자에게 응답을 받기 위해 조금 더 기다려야 만한다고 상상하기 때문에, 나는 C#에서왔다. 그리고 많은 것을 기다리는 HttpClient 클래스 Android보다 응답 시간이 오래 걸립니다.안드로이드에서 OKHttp를 사용하여 응답 시간을 처리하는 방법
public class PostBase {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final MediaType urlEnconde = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
//private String Url="http://192.168.137.1:3000/";
OkHttpClient client = new OkHttpClient();
int POST(final PostModel model) throws IOException{
Gson gson = new Gson();
String modelJson = gson.toJson(model);
RequestBody body = RequestBody.create(JSON,modelJson);
Request request = new Request.Builder()
.url(URLb.GetUrl() + "api/gone/POST")
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.code();
}
그리고 내 활동 : 여기 내 코드는
public void MakePost(final PostModel model){
new AsyncTask<Void,Void,String>(){
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(SignUpActivity.this,R.style.Theme_MyDialog);
progressDialog.setMessage("Sending....");
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
try{
PostBase postBase = new PostBase();
statusCode = postBase.POST(model);
if(statusCode == 201){
return "OK";
}
if(statusCode == 409){
return "Conflict";
}
else {
return "ERROR";
}
}catch (IOException e){
e.printStackTrace();
return "ERROR";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
switch (s) {
case "OK":
progressDialog.dismiss();
AlertDialog.Builder builder1 = new AlertDialog.Builder(SignUpActivity.this);
builder1.setTitle("Gone");
builder1.setMessage("Fine");
builder1.setCancelable(true);
builder1.setPositiveButton(
"ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
//finish();
break;
case "Conflict":
progressDialog.dismiss();
new DialogMessage(SignUpActivity.this, "already exist");
break;
default:
progressDialog.dismiss();
new DialogMessage(SignUpActivity.this, "something went wrong");
break;
}
}
}.execute();
}
감사합니다!
그러한 방법 ... –
죄송합니다, 그것을 시도 OkHttpClient eagerClient = client.newBuilder() .readTimeout (500, TimeUnit.MILLISECONDS) .build가 (없다); – Pein