使用AsyncTask的和namevaluepairs中HTTP POST [英] Http post using AsyncTask and NameValuePairs

查看:134
本文介绍了使用AsyncTask的和namevaluepairs中HTTP POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要一些数据发送到所述服务器的应用程序。我创建一个Connection类来处理的过程。下面是我的code:

 私有类连接扩展的AsyncTask< ArrayList的<的NameValuePair>中太虚,太虚> {    保护无效doInBackground(ArrayList的<&的NameValuePair GT; ... namevaluepairs中){        尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(http://www.xxxx.com/postdata.php);
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));         //执行HTTP POST请求
            HTT presponse响应= httpclient.execute(httppost);            Log.i(POSTDATA,response.getStatusLine()的toString());
        }
        最后{}    }}

不过,我得到了以下错误:构造UrlEn codedFormEntity(ArrayList的[])是不确定的。我是新来的Andr​​oid和我不知道是什么引起的错误。非常感谢!


解决方案

 私有类连接扩展的AsyncTask< ArrayList的<的NameValuePair>中太虚,太虚> {    保护无效doInBackground(ArrayList的<&的NameValuePair GT; ... namevaluepairs中){
        //获取零指数namevaluepairs中,并用它来发布
        ArrayList的<&的NameValuePair GT; nvPairs = namevaluepairs中[0];        尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(http://www.xxxx.com/postdata.php);
            httppost.setEntity(新UrlEn codedFormEntity(nvPairs));         //执行HTTP POST请求
            HTT presponse响应= httpclient.execute(httppost);            Log.i(POSTDATA,response.getStatusLine()的toString());
        }
        最后{}    }}

I have an app that needs to send some data to the server. I create a Connection class to handle the process. Below is my code:

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{}

    }

}

However, I get the following error: "The constructor UrlEncodedFormEntity(ArrayList[]) is undefined". I'm new to Android and I have no idea what's causing the error. Many thanks!

解决方案

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{}

    }

}

这篇关于使用AsyncTask的和namevaluepairs中HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆