开发者

Android, sending XML string to server via HTTP

I have recently started doing Android programming. I have come across a problem. Currently I have built a XML string request called 'sendData'. This sendData needs to be sent to the server to validate if the user account is valid or not.

Currently when I try to send the request, I make contact with the server, but I get a default generic response error back which makes me believe that the XML isn't being sent correcrtly.

public void postD开发者_运维问答ata(String sendData) {
        // Create a new HttpClient and Post Header
        System.out.println("SENDDATA" + sendData);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www......");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(0);
            nameValuePairs.add(new BasicNameValuePair(sendData, sendData));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

            inputStreamToString(response.getEntity().getContent());

        } 
        catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
        }
    } 

The above is the code I am using to send the data. I believe the problem is happening within the 'try' section, possibly. I have searched for similar problems on this forum and cannot find the answer I was looking for.

Any help will be appreciated.

Thanks,

EDIT:

In the log cat. When I print the string off, it looks like this.

07-19 15:35:36.642: INFO/System.out(2204): <Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.0" xsi:noNamespaceSchemaLocation="http://www.example.com">
07-19 15:35:36.642: INFO/System.out(2204): <Identification>
07-19 15:35:36.642: INFO/System.out(2204): <UserID>username@example.com</UserID>
07-19 15:35:36.642: INFO/System.out(2204): <Password>password</Password>
07-19 15:35:36.642: INFO/System.out(2204): </Identification>
07-19 15:35:36.642: INFO/System.out(2204): <Service>
07-19 15:35:36.652: INFO/System.out(2204): <ServiceName>AccountVerify</ServiceName>
07-19 15:35:36.652: INFO/System.out(2204): <ServiceDetail/>
07-19 15:35:36.652: INFO/System.out(2204): </Service>

07-19 15:35:36.652: INFO/System.out(2204):

Each element has it's own line in the LogCat. Could this be the problem?


I have got to the answer. The problem was that no information was being sent in the actual request upon looking further. The solution was to remove the array and replace it by setting a new Entity. The changed / working code is below:

        try {
            StringEntity se = new StringEntity(sendData);
            httppost.setEntity(se);
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            System.out.println("Message Sent!");
            inputStreamToString(response.getEntity().getContent());
            } 


I strongly suggest that you use Fiddler Web Debugger to help you detect what you are sending. This is a great tool and will save you a lot of time.

There are a couple of errors issues with the code you have posted.

The arraylist you are creating has a size of 0, this would be more efficient to set to 1 (as you are adding only one NameValuePair

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

The first BasicNameValuePair constructor parameter is wrong and should be some kind of key name

nameValuePairs.add(new BasicNameValuePair("mySendData", sendData));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜