Quelqu'un peut-il me dire comment puis-je obtenir la réponse de l'API en utilisant la méthode HttpHandler en donnant les paramètres d'en-tête? Voici mon HTTPHandler java recueil HSC Intégration API Android
package com.example.addvehicle;
import android.util.Log;
import android.widget.ListView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try{
URL url = new URL("http://garage.kaptastech.mobi/api/5k/users/vehicle");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
}catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();``
}
}`
i besoin d'ajouter deux paramètres d'en-tête 1 -> id 2 -> imei Comment puis-je ajouter que dans mon fichier java HttpHandler ci-dessus? pls quelqu'un aider me.Many merci à l'avance
msdev, j'ai essayé. mais je n'ai toujours pas reçu de réponse – Arun
la connexion url http utilisée provoque une exception: android.os.NetworkOnMainThreadException ,, et c'est pourquoi vous devez utiliser une tâche asynchrone – msdev16