j'ai utilisé Http pour la demande de réseau, mais http est lent maintenant je veux utiliser okhttp au lieu de http comment puis-je convertir http à okhttp mon code est ci-dessousComment convertir Http pour ok http
public class Http {
public String postRequest(String endPoint, String body) {
URL url;
Log.d("Waseem", "Calling URL:" + endPoint);
HttpURLConnection connection = null;
String response = "";
try {
url = new URL(endPoint);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//send data to server over the connection.
OutputStream outputStream = connection.getOutputStream();
outputStream.write(body.getBytes());
outputStream.flush();
outputStream.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseLine;
while ((responseLine = bufferedReader.readLine()) != null) {
// Log.d(MainActivity.LOG_TAG,"Response: "+responseLine);
response += responseLine;
}
//Log.d(MainActivity.LOG_TAG,"Response: "+response);
connection.disconnect();
} catch (Exception exp) {
//Log.d(MainActivity.LOG_TAG,"Exception: "+exp.getMessage());
exp.printStackTrace();
response = "{\"result\":false,\"message\":\"Exception: " + exp.getMessage() + "\"}";
}
if (connection != null) {
connection.disconnect();
}
return response;
}
C'est activité java où j'ai fait une demande http comment peut changer ce code ok http
private Http http;
private String remoteMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
emailLogin= (EditText) findViewById(R.id.etEmailLog);
passwordLogin= (EditText) findViewById(R.id.etPassLog);
http=new Http();
login= (TextView) findViewById(R.id.btnLogin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (emailLogin.getText().toString().length()>3 &&
passwordLogin.getText().toString().length()>4){
emailText=emailLogin.getText().toString();
passwordText=passwordLogin.getText().toString();
new AsyncTask<Void, Integer, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
//progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(Void... params) {
String response;
String body="email="+emailText+"&password="+passwordText;
response=http.postRequest(HttpConfigs.URL_AUTH,body);
return response;
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
try {
final JSONObject responseJson = new JSONObject(response);
remoteMessage=responseJson.getString("message");
if (responseJson.getBoolean("result")){
showMessage(remoteMessage);
JSONObject data = responseJson.getJSONObject("data");
Log.d("WaseemTest","Data"+data);
}else{
showMessage(remoteMessage);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}.execute();
}
}
});
}
Avez-vous lu les tutoriels okhttp? –
'utilisé Http pour la demande réseau'. Non. C'est HttpUrlConnection. 'mais http est lent'. Non. HttpUrlConnection n'est pas lent. – greenapps