Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package com.example.orlydb;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URLEncoder;
  7.  
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.protocol.BasicHttpContext;
  14. import org.apache.http.protocol.HttpContext;
  15.  
  16. import android.os.AsyncTask;
  17. import android.util.Log;
  18. import android.widget.Toast;
  19.  
  20. public class GetSearchesClass extends AsyncTask<Search, Integer, String> {
  21.  
  22. @Override
  23. protected String doInBackground(Search... params) {
  24. String result;
  25. Search search = params[0];
  26.  
  27. try {
  28. result = getSearches(search.URL, search.search);
  29. } catch (Exception e) {
  30. return null;
  31. }
  32. return result;
  33. }
  34.  
  35. String getSearches(String URL, String search) throws ClientProtocolException, IOException {
  36. String encoded = URLEncoder.encode(search, "UTF-8");
  37.  
  38. Log.d("kodadurl",encoded);
  39.  
  40. HttpClient httpClient = new DefaultHttpClient();
  41. HttpContext localContext = new BasicHttpContext();
  42. HttpGet httpGet = new HttpGet(URL + encoded);
  43. HttpResponse response = httpClient.execute(httpGet, localContext);
  44. response.getStatusLine().getStatusCode();
  45.  
  46. if(response == (int)SC_BAD_GATEWAY) {
  47. Toast.makeText(getApplicationContext(), "502 Bad Gateway",Toast.LENGTH_LONG).show();
  48. }
  49.  
  50. Log.d("kodadurl", (URL + encoded));
  51.  
  52. String result = "";
  53.  
  54. BufferedReader reader = new BufferedReader(
  55. new InputStreamReader(response.getEntity().getContent()));
  56.  
  57. String line = null;
  58. while ((line = reader.readLine()) != null) {
  59. result += line + "\n";
  60. }
  61. return result;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement