How to send data as a form via HttpPost to server in Android?

I am working on an android project in which i need to send two xml as parameters to server using post method(i.e. i want to send as form). I am tried to send data by using following code but its not working. No data in the remote database.

private void postFormData(List<DataItem> ti,String ex,String getExpensesXml)
{    
//Create a new Http Client 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
try
{ 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("sync","true"));
    nameValuePairs.add(new BasicNameValuePair("tt",ti));
    nameValuePairs.add(new BasicNameValuePair("te",ex)); 

    UrlEncodedFormEntity form; 
    form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");  
    httppost.setEntity(form); 

    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity();  
    String line = EntityUtils.toString(entity);    
    System.out.println(line);

} catch (ClientProtocolException e) {
       e.printStackTrace();
    } catch (IOException e) {
       e.printStackTrace();
    } 
}

I couldn't find what was the problem. It would be great if anyone manage to find the problem and suggest me the solution.

I have two more question? Am i trying the correct code? Is there any other way to send xml data to server via Form?

Thanks in advance

Answers


Finally i found the solution , I used the following code to send the data as a from to server

URL siteUrl;
    try {
        siteUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) siteUrl
                .openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        String content1 = ""; 

        Set getkey = param.keySet();
        Iterator keyIter = getkey.iterator();
        String content = "";
        for (int i = 0; keyIter.hasNext(); i++) {
            Object key = keyIter.next();
            if (i != 0) {
                content += "&";
            }
            content += key + "=" + param.get(key);
            System.out.println("Content" + content);
        }

        System.out.println(content);
        out.writeBytes(content.trim());
        out.flush();
        out.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        String line = "";
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();


    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    db.close();
}

It may helpful for some one like me.


Faced same problem. But Finally managed to Post data to Server. Your code is absolutely the way it should be except that you have to encode your String to Base64 and then pass it to NameValuePair. At Server side you have to decode your String back from Base64. It will work. I can share code if you need.


Need Your Help

Visual Studio and Netbeans

netbeans visual-studio-2013

Is it possible to put a Visual Studio project into Net beans or do you have to remake the whole project in Net beans?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.