Guest User

Untitled

a guest
Oct 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. public static Map<String,Object> post(String url, Map<String,String> args) throws UnsupportedEncodingException{
  2. Map<String,Object> map = new HashMap<String,Object>();
  3. HttpParams params = new BasicHttpParams();
  4. for(String kv : args.keySet()){
  5. params.setParameter(kv, args.get(kv));
  6. Log.i("_params", kv + " -> " + args.get(kv));
  7. }
  8.  
  9. Log.i("_data", params.toString());
  10.  
  11. BufferedReader in = null;
  12. try {
  13. HttpClient client = new DefaultHttpClient();
  14. HttpPost request = new HttpPost(url);
  15.  
  16. request.setParams(params);
  17. HttpResponse response = client.execute(request);
  18. in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  19. StringBuffer sb = new StringBuffer("");
  20. String line = "";
  21. String NL = System.getProperty("line.separator");
  22. while ((line = in.readLine()) != null) {
  23. sb.append(line + NL);
  24. }
  25. in.close();
  26. String page = sb.toString();
  27. Log.i("_http_response", page);
  28.  
  29. map.put("status_code", Integer.toString(response.getStatusLine().getStatusCode()));
  30. map.put("body", page);
  31.  
  32. } catch (IllegalStateException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. } catch (IOException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. } finally {
  39. if (in != null) {
  40. try {
  41. in.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47.  
  48.  
  49. return map;
  50. }
Add Comment
Please, Sign In to add comment