rachmadi

Test

Jan 28th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package info.rekayasa.test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.UnsupportedEncodingException;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import android.app.Activity;
  14. import android.os.AsyncTask;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20.  
  21. public class MainActivity extends Activity {
  22.  
  23.     static InputStream is = null;
  24.     String URL = "http://comptalk.de/group/get.php?user=reditec&method=01&pw=bliblablub";
  25.     String result;
  26.     TextView tvResult;
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.         tvResult = (TextView) findViewById(R.id.tvResult);
  33.        
  34.         new GettingData().execute();
  35.        
  36.     }
  37.    
  38.     private class GettingData extends AsyncTask<Void, Void, String>{
  39.  
  40.         @Override
  41.         protected String doInBackground(Void... params) {
  42.  
  43.         try {
  44.                 // Construct the client and the HTTP request.
  45.                 DefaultHttpClient httpClient = new DefaultHttpClient();
  46.                 HttpPost httpPost = new HttpPost(URL);
  47.  
  48.                 // Execute the POST request and store the response locally.
  49.                 HttpResponse httpResponse = httpClient.execute(httpPost);
  50.                 // Extract data from the response.
  51.                 HttpEntity httpEntity = httpResponse.getEntity();
  52.                 // Open an inputStream with the data content.
  53.                 is = httpEntity.getContent();
  54.             } catch (UnsupportedEncodingException e) {
  55.                 e.printStackTrace();
  56.             } catch (ClientProtocolException e) {
  57.                 e.printStackTrace();
  58.             } catch (IOException e) {
  59.                 e.printStackTrace();
  60.             }
  61.            
  62.             try {
  63.                 // Create a BufferedReader to parse through the inputStream.
  64.                 BufferedReader reader = new BufferedReader(new InputStreamReader(
  65.                         is, "iso-8859-1"), 8);
  66.                 // Declare a string builder
  67.                 StringBuilder sb = new StringBuilder();
  68.                 // Declare a string to store the result
  69.                 String line = null;
  70.                
  71.                 // Build the string until null.
  72.                 while ((line = reader.readLine()) != null) {
  73.                     sb.append(line + "\n");
  74.                 }              
  75.                 // Close the input stream.
  76.                 is.close();
  77.                 // Convert the string builder data to an actual string.
  78.                 result = sb.toString();
  79.                 return result;
  80.                
  81.             } catch (Exception e) {
  82.                 Log.e("Buffer Error", "Error converting result " + e.toString());
  83.             }
  84.             return null;
  85.         }  
  86.        
  87.         @Override
  88.         protected void onPostExecute(String res) {
  89.             tvResult.setText(res);
  90.             Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment