Advertisement
Guest User

Untitled

a guest
Aug 13th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package com.berko.tictactoe;
  2.  
  3.  
  4. import java.io.IOException;
  5.  
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.util.EntityUtils;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16.  
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.os.AsyncTask;
  20. import android.os.Bundle;
  21. import android.view.Menu;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. public class MainActivity extends Activity {
  26.    
  27.     static Context context;
  28.     HttpClient client;
  29.     TextView t;
  30.     JSONObject json;
  31.    
  32.     final static String URL = "http://nirberko.info/android/index.php";
  33.    
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_main);
  38.         context = this;
  39.        
  40.         t = (TextView)findViewById(R.id.textView1);
  41.        
  42.         client = new DefaultHttpClient();
  43.         new Read().execute("bla");
  44.     }
  45.  
  46.     @Override
  47.     public boolean onCreateOptionsMenu(Menu menu) {
  48.         // Inflate the menu; this adds items to the action bar if it is present.
  49.         getMenuInflater().inflate(R.menu.main, menu);
  50.         return true;
  51.     }
  52.    
  53.     public JSONObject lastTweet(String function) throws ClientProtocolException, IOException, JSONException {
  54.         StringBuilder url = new StringBuilder(URL);
  55.        
  56.         HttpGet get = new HttpGet(url.toString());
  57.         HttpClient httpclient = new DefaultHttpClient();
  58.         HttpResponse response = httpclient.execute(get);
  59.        
  60.         int status = response.getStatusLine().getStatusCode();
  61.        
  62.         if (status == 200) {
  63.             HttpEntity e = response.getEntity();
  64.             String data = EntityUtils.toString(e);
  65.             JSONArray timeline = new JSONArray(data);
  66.             JSONObject last = timeline.getJSONObject(0);
  67.             t.setText("good");
  68.             return null;
  69.         } else {
  70.             t.setText("error");
  71.             return null;
  72.         }
  73.     }
  74.    
  75.     public class Read extends AsyncTask<String, Integer, String> {
  76.  
  77.         @Override
  78.         protected String doInBackground(String... arg0) {
  79.             // TODO Auto-generated method stub
  80.             try {
  81.                 json = lastTweet("bla");
  82.             } catch (ClientProtocolException e) {
  83.                 // TODO Auto-generated catch block
  84.                 e.printStackTrace();
  85.             } catch (IOException e) {
  86.                 // TODO Auto-generated catch block
  87.                 e.printStackTrace();
  88.             } catch (JSONException e) {
  89.                 // TODO Auto-generated catch block
  90.                 e.printStackTrace();
  91.             }
  92.             return null;
  93.         }
  94.  
  95.         @Override
  96.         protected void onPostExecute(String result) {
  97.             // TODO Auto-generated method stub
  98.             super.onPostExecute(result);
  99.         }
  100.        
  101.     }
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement