Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.rekayasa.test;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- static InputStream is = null;
- String URL = "http://comptalk.de/group/get.php?user=reditec&method=01&pw=bliblablub";
- String result;
- TextView tvResult;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvResult = (TextView) findViewById(R.id.tvResult);
- new GettingData().execute();
- }
- private class GettingData extends AsyncTask<Void, Void, String>{
- @Override
- protected String doInBackground(Void... params) {
- try {
- // Construct the client and the HTTP request.
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(URL);
- // Execute the POST request and store the response locally.
- HttpResponse httpResponse = httpClient.execute(httpPost);
- // Extract data from the response.
- HttpEntity httpEntity = httpResponse.getEntity();
- // Open an inputStream with the data content.
- is = httpEntity.getContent();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- // Create a BufferedReader to parse through the inputStream.
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- is, "iso-8859-1"), 8);
- // Declare a string builder
- StringBuilder sb = new StringBuilder();
- // Declare a string to store the result
- String line = null;
- // Build the string until null.
- while ((line = reader.readLine()) != null) {
- sb.append(line + "\n");
- }
- // Close the input stream.
- is.close();
- // Convert the string builder data to an actual string.
- result = sb.toString();
- return result;
- } catch (Exception e) {
- Log.e("Buffer Error", "Error converting result " + e.toString());
- }
- return null;
- }
- @Override
- protected void onPostExecute(String res) {
- tvResult.setText(res);
- Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment