Advertisement
Guest User

service

a guest
Apr 27th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. HttpParams httpParameters = new BasicHttpParams();
  2.  
  3. int timeoutConnection = 10000;
  4. HttpConnectionParams.setConnectionTimeout(httpParameters,
  5. timeoutConnection);
  6.  
  7. int timeoutSocket = 10000;
  8. HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
  9.  
  10. httpclient = new DefaultHttpClient(httpParameters);
  11. HttpPost httppost = new HttpPost(url);
  12. String responseBody = "";
  13. HttpResponse response = null;
  14.  
  15. try {
  16.  
  17. String base64EncodedCredentials = "Basic "
  18. + Base64.encodeToString(
  19. (userName + ":" + password).getBytes(),
  20. Base64.NO_WRAP);
  21.  
  22. httppost.setHeader("Authorization", base64EncodedCredentials);
  23.  
  24. response = httpclient.execute(httppost);
  25.  
  26. if (response.getStatusLine().getStatusCode() == 200) {
  27. Dbg.e("new ok response");
  28. responseBody = EntityUtils.toString(response.getEntity());
  29.  
  30. // aggiungo queste stringhe per comodità di parse via Gson
  31. responseBody = "{allnews:" + responseBody + "}";
  32. Gson gson = new Gson();
  33.  
  34. ResponseNews reponseNews = gson.fromJson(
  35. responseBody.toString(), ResponseNews.class);
  36.  
  37. mycallback.setResponse(reponseNews);
  38. } else {
  39. Dbg.e("response not ok Something went wrong : >200");
  40.  
  41. AimError aimerror = new AimError();
  42. aimerror.typeError = AimCostants.Error.ERROR_CONNECTION;
  43. mycallback.setErrorResponse(aimerror);
  44.  
  45. }
  46.  
  47. } catch (ClientProtocolException e) {
  48. e.printStackTrace();
  49.  
  50. AimError error = new AimError();
  51. error.typeError = AimCostants.Error.ERROR_CONNECTION;
  52. mycallback.setErrorResponse(error);
  53. } catch (IOException e) {
  54.  
  55. AimError aimerror = new AimError();
  56.  
  57. mycallback.setErrorResponse(aimerror);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement