Android App - 在两个活动之间传输字符串数据

时间:2016-04-25 19:14:47

标签: java android

我正在构建一个Android应用程序,我想将更新的内容添加到网站上的活动中。这是我到目前为止所做的。它是谷歌的一个word文档,它只是拉取它的页面源,而不是它内部的文本。

package com.example.it5.foothillers;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

    Button button;
    Button button2;
    Button button3;

    public TextView textViewData2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            button = (Button)findViewById(R.id.button);
            button.setOnClickListener(this);

//                button.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    new newsTask().execute("https://docs.google.com/document/d/1fi_nnm_d4yPbe6fZVNvCrklU5Dq4fI8ybWRC64hK6qc/edit?usp=sharing");
//
//                }
//            });

            textViewData2 = (TextView)findViewById(R.id.textViewData2);
            button2 = (Button)findViewById(R.id.button2);
            button2.setOnClickListener(this);
            button3 = (Button)findViewById(R.id.button3);
            button3.setOnClickListener(this);

    }




    private void buttonClick() {

//        startActivity(new Intent("it5.foothillers.news"));

    }
    private void button2Click() {

        startActivity(new Intent("it5.foothillers.sports"));
    }
    private void button3Click() {
        startActivity(new Intent("it5.foothillers.events"));
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.button:
                buttonClick();
                new newsTask().execute("https://docs.google.com/document/d/1fi_nnm_d4yPbe6fZVNvCrklU5Dq4fI8ybWRC64hK6qc/edit?usp=sharing");

                // Something
                break;
            case R.id.button2:
                button2Click();
                break;
            case R.id.button3:
                button3Click();
                break;

        }

    }

    public static void callBackDataFromAsyncTask(String result) {
    }


    private class newsTask extends AsyncTask<String, String, String>{


        @Override
        protected String doInBackground(String... params) {

            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(params[0]);

                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();
                // BufferedReader might not be needed
                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line ="";

                while ((line = reader.readLine())!= null){
                    buffer.append(line);
                }

                return buffer.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(connection != null)
                    connection.disconnect();
                connection.disconnect();
                try {
                    if(reader != null){
                        reader.close();
                    }
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            Intent intent = new Intent(MainActivity.this, news.class);
            intent.putExtra("some_key", params);
            intent.putExtra("some_other_key", "a value");
            startActivity(intent);

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            textViewData2.setText(result);

        }

    }
    // end of private class

}

我想传输字符串结果。

0 个答案:

没有答案
相关问题