Advertisement
Guest User

PleaseJson

a guest
Sep 14th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. @Override
  2.     protected void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.         setContentView(R.layout.activity_festum);
  5.         //////////////////////////////////////////
  6.         Button HappyButton = (Button)findViewById(R.id.Happy); 
  7.         HappyButton.setText("Старт");
  8.  
  9.         final TextView MyText = (TextView) findViewById(R.id.textView1);
  10.         /////////////////////////////////////////
  11.        
  12.         //######################################################
  13.             OnClickListener oclBtnHappy = new OnClickListener() {
  14.             @Override
  15.                 public void onClick(View v) {
  16.                         MyText.setText(getMyJSONPlease());
  17.                 }
  18.             };
  19.             //######################################################   
  20.         HappyButton.setOnClickListener(oclBtnHappy);
  21.     }
  22.  
  23.  
  24.    
  25.     public String getMyJSONPlease(){
  26.         String result = new String("");
  27.        
  28.             try{
  29.            
  30.             HttpGet httpGet = new HttpGet("http://festum.url.ph/api/congrats/getRandomCongrats.json");
  31.             HttpParams httpParameters = new BasicHttpParams();
  32.             int timeoutConnection = 3000;
  33.             HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
  34.             int timeoutSocket = 5000;
  35.             HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
  36.  
  37.             DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
  38.             HttpResponse response = httpClient.execute(httpGet);
  39.             httpClient.setParams(httpParameters);
  40.            
  41.  
  42.             BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"utf-8"));
  43.             StringBuilder sb = new StringBuilder();
  44.          
  45.             String line;
  46.             while((line = reader.readLine()) != null) {
  47.                 sb.append(line + System.getProperty("line.separator"));
  48.             }
  49.             reader.close();  
  50.             result = sb.toString();
  51.            
  52.             JSONObject jObject = new JSONObject(result);
  53.          
  54.             //String CongratsID = (String) jObject.getString("id");
  55.             //String Congrats = (String) jObject.get("text");
  56.  
  57.          
  58.             } catch (org.apache.http.client.ClientProtocolException e) {
  59.                     result = "ClientProtocolException: " + e.getMessage();
  60.             } catch (IOException e) {
  61.                     result = "IOException: " + e.getMessage();
  62.             } catch (Exception e) {
  63.                     result = "Exception: " + e.getMessage();
  64.             }
  65.  
  66.         return result;
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement