0

我有一个需要向服务器发送一些数据的应用程序。我创建了一个 Connection 类来处理这个过程。下面是我的代码:

private class Connection extends AsyncTask<ArrayList<NameValuePair>, Void, Void>{

    protected Void doInBackground(ArrayList<NameValuePair>... nameValuePairs){

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.xxxx.com/postdata.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            Log.i("postData", response.getStatusLine().toString());
        }
        finally{}

    }

}

但是,我收到以下错误:“构造函数 UrlEncodedFormEntity(ArrayList[]) 未定义”。我是 Android 新手,我不知道是什么导致了错误。非常感谢!

4

1 回答 1

1
private class Connection extends AsyncTask<ArrayList<NameValuePair>, Void, Void>{

    protected Void doInBackground(ArrayList<NameValuePair>... nameValuePairs){
        // get zero index of nameValuePairs and use that to post
        ArrayList<NameValuePair> nvPairs = nameValuePairs[0];

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.xxxx.com/postdata.php");
            httppost.setEntity(new UrlEncodedFormEntity(nvPairs));

         // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            Log.i("postData", response.getStatusLine().toString());
        }
        finally{}

    }

}
于 2013-07-30T15:21:20.243 回答