Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. [HttpPost]
  2. public HttpResponseMessage RetrieveString(String longString)
  3. {
  4. var resp = new HttpResponseMessage()
  5. {
  6. Content = new StringContent("{"ResultString":"" + longString + ""}")
  7. };
  8. resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  9. return resp;
  10. }
  11.  
  12. protected String doInBackground(String... params) {
  13.  
  14. HttpURLConnection urlConnection = null;
  15. BufferedReader reader = null;
  16. String JSONStr = null;
  17.  
  18. try {
  19. final String URL = "http://172.27.179.197:7837/api/TestCon/RetrieveString?";
  20.  
  21. Uri builtUri = Uri.parse(URL).buildUpon()
  22. .appendQueryParameter("longString", params[0]).build();
  23.  
  24. URL url = new URL(builtUri.toString());
  25.  
  26. urlConnection = (HttpURLConnection) url.openConnection();
  27. urlConnection.setRequestMethod("POST");
  28. urlConnection.setRequestProperty("connection", "keep-alive");
  29. urlConnection.setRequestProperty("Content-Type", "multipart/form-data");
  30. urlConnection.connect();
  31.  
  32. InputStream inputStream = urlConnection.getInputStream();
  33. StringBuffer buffer = new StringBuffer();
  34. if (inputStream == null) {
  35. return null;
  36. }
  37. reader = new BufferedReader(new InputStreamReader(inputStream));
  38.  
  39. String line;
  40. while ((line = reader.readLine()) != null) {
  41. buffer.append(line + "n");
  42. }
  43.  
  44. if (buffer.length() == 0) {
  45. return null;
  46. }
  47. JSONStr = buffer.toString();
  48. } catch (IOException e) {
  49. Log.e(LOG_TAG, "Error ", e);
  50. return null;
  51. } finally {
  52. if (urlConnection != null) {
  53. urlConnection.disconnect();
  54. }
  55. if (reader != null) {
  56. try {
  57. reader.close();
  58. } catch (final IOException e) {
  59. Log.e(LOG_TAG, "Error closing stream", e);
  60. }
  61. }
  62. }
  63. return JSONStr;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement