通过ASYNC任务的参数

时间:2014-03-12 07:28:08

标签: android-asynctask

我使用过httppost来发送和检索来自网站的数据。但是当我运行程序时,我没有得到所需的数据

package com.example.im3;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Homepage extends Activity {

    Button login1, login2;
    LinearLayout l1;
    RelativeLayout r1;
    EditText un, pw;
    private static final String TAG = "MyActivity";
    Toast toast;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        login2 = (Button) findViewById(R.id.button1);
        login1 = (Button) findViewById(R.id.Login1);
        l1 = (LinearLayout) findViewById(R.id.Homepage);
        r1 = (RelativeLayout) findViewById(R.id.Loginpage2);
        un = (EditText) findViewById(R.id.un);
        pw = (EditText) findViewById(R.id.pw);
        l1.setVisibility(l1.VISIBLE);
        r1.setVisibility(r1.GONE);
        login2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                String s1 = un.getText().toString();
                String s2 = pw.getText().toString();

                if(s1.matches("")){

                    Toast.makeText(Homepage.this, "enter something", toast.LENGTH_LONG).show();
                }
                Log.d(TAG, "second login page");

                Abc task = new Abc();
                task.execute(s1, s2);
            }
        });
        login1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                l1.setVisibility(v.GONE);
                r1.setVisibility(v.VISIBLE);
                Log.d(TAG, "home page");

            }
        });
    }

    private class Abc extends AsyncTask<String, String, String> {
        String s1,s2;

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            s1=un.getText().toString();
            s2=pw.getText().toString();
            String urlParameters = s1,s2;
            String request = "https://beta135.hamarisuraksha.com/web/webservice/HamariSurakshaMobile.asmx/getIMSafeAccountInfoOnLogon";
            URL url;

            try {
                url = new URL(request);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setInstanceFollowRedirects(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded;");// boundary="+CommonFunctions.boundary
                connection.setUseCaches(false);

                DataOutputStream wr = new DataOutputStream(
                        connection.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();


                int responseCode = connection.getResponseCode();
                /*
                 * System.out.println("\nSending 'POST' request to URL : " +
                 * url); System.out.println("Post parameters : " +
                 * urlParameters);
                 */
                System.out.println("Response Code : " + responseCode);

                InputStream errorstream = connection.getErrorStream();

                BufferedReader br = null;
                if (errorstream == null) {
                    InputStream inputstream = connection.getInputStream();
                    br = new BufferedReader(new InputStreamReader(inputstream));
                } else {
                    br = new BufferedReader(new InputStreamReader(errorstream));
                }
                String response = "";
                String nachricht;
                while ((nachricht = br.readLine()) != null) {
                    response += nachricht;
                }

                // print result
                // System.out.println(response.toString());
                return response.toString();

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } catch (ProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
             protected void onPostExecute(String response) 
                {
                    // TODO Auto-generated method stub              
                    super.onPostExecute(response);
                    Log.d(TAG,"on post execute");
                    Toast.makeText(Homepage.this, "finally", toast.LENGTH_LONG).show();


        }

    }
}

我已经使用httppost发送和检索来自网站的数据。但是当我运行程序时,我没有获得所需的数据 我想将s1和s2参数传递给htp post方法。但是我得知道怎么做

1 个答案:

答案 0 :(得分:0)

您的参数作为doInBackground方法中的参数String... params。所以你可以像这样使用它们:

protected String doInBackground(String... params) {
    s1=params[0];
    s2=params[1];
    //...
}

而不是:

protected String doInBackground(String... params) {
    s1=un.getText().toString();
    s2=pw.getText().toString();
    //...
}