如何从android发送数据?

时间:2014-02-11 21:35:59

标签: java android

这是我的OnClick方法:

ib_wyslij.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            new MyAsyncTask().execute(zam.klient.getNazwa(),zam.klient.getNip(),zam.klient.getAdres());


        }
    });

这是我的AsycTask课程:

public class MyAsyncTask extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            postData(null, null, null);
            return null;

        }

        @Override
        protected void onPostExecute(Void result) {

            Toast.makeText(getApplicationContext(), "command sent",
                    Toast.LENGTH_LONG).show();
        }

        public void postData(String nazwa, String adres, String nip) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://www.msinzynierka.cba.pl/executeConn.php");


            try {
                // Add your data

                KlientNameValuePairs = new ArrayList<NameValuePair>();
                KlientNameValuePairs.add(new BasicNameValuePair("Kli_imie", nazwa));
                KlientNameValuePairs.add(new BasicNameValuePair("Kli_adres", adres));
                KlientNameValuePairs.add(new BasicNameValuePair("Kli_nr_telefonu",nip));

                httppost.setEntity(new UrlEncodedFormEntity(
                        KlientNameValuePairs));



                HttpResponse response = httpclient.execute(httppost);

                            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

我想问一下如何让它有效?我在这里NullPointerException: 但价值观在。我为所有人做了祝酒,价值观很好。我怎样才能使它有效?欢迎任何想法

new MyAsyncTask().execute(zam.klient.getNazwa(),zam.klient.getNip(),zam.klient.getAdres());

1 个答案:

答案 0 :(得分:0)

您在代码中传递了AsyncTask中的3个空值:

@Override
protected Void doInBackground(String... params) {
    // TODO Auto-generated method stub
    postData(null, null, null);
    return null;

}

这应该是:postData(params[0], params[1], params[2]);

此外,由于您在调用执行方法时如何排列字符串,因此您可能希望将参数重新排列在postData(String nazwa, String adres, String nip)postData(String nazwa, String nip, String adres)