Vi posto alcuni pezzi di codice nella speranza che possiate avere una panoramica migliore sul mio problema.
Questi sono due metodi della classe che ho creato per gestire la comunicazione http.
Il primo effettua la connessione e fa un invio POST, il secondo riceve i dati e li ritorna in forma di stringa.
La classe l'ho chiamata HttpComm.
codice:
public static HttpResponse doPost(String url, Map<String, String> postData) throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
if (postData != null && postData.isEmpty() == false) {
List<NameValuePair> dataList = new ArrayList<NameValuePair>(postData.size());
Iterator<String> iKeys = postData.keySet().iterator();
while (iKeys.hasNext()) {
String k = iKeys.next();
String v = postData.get(k);
dataList.add(new BasicNameValuePair(k, v));
}
httpPost.setEntity(new UrlEncodedFormEntity(dataList));
}
return httpClient.execute(httpPost);
}
public static String getResponse(HttpResponse r) throws IOException {
InputStream is = r.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
String res = "";
res = EntityUtils.toString(r.getEntity());
// res = baf.toByteArray().toString();
return res /*new String(baf.toByteArray())*/;
}
Qui invece è la parte di codice dove utilizzo la classe per effettuare il login.
codice:
String url = "https://url";
String resp = "";
Map<String, String> accountValues = new HashMap<String, String>();
accountValues.put("username_id", params[0]);
accountValues.put("password_id", params[1]);
try {
HttpResponse r = HttpComm.doPost(url, accountValues);
resp = HttpComm.getResponse(r);
Log.e("RESPONSE_CODE: ", Integer.toString(r.getStatusLine().getStatusCode()));
Log.e("RESPONSE_STRING: ", resp);
}
catch (Exception ex){
Log.e("!!!ERRORE!!!: ", ex.getMessage());
}
La funzione genera una eccezione nel blocco TRY e l'errore è il seguente:
"The target server failed to respond".