dido
dido

Reputation: 3407

Android Web Services Using Rest - Error In SOAP Message

I'm having a problem with getting a simple output from a web service using Android RESTful consumer. Below is my code and my output. However, I cannot seem to get a value returned from this web service. http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

I'd greatly appreciate it if anyone can help me figuring out why it says in the SOAP message

"Server was unable to process request. ---> Data at the root level is      
invalid"

My Code Compiles And Runs Fine

public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(
                "http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit");

        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("param1", "77"));
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);

        HttpResponse response = client.execute(request);

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String page = sb.toString();
        // Log.i(tag, page);
        System.out.println(page);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

My Output

09-08 14:25:25.383: I/System.out(1620): <?xml version="1.0" encoding="utf-8"?>   
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code> 
<soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text   
xml:lang="en">Server was unable to process request. ---&gt; Data at the root level is  
invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault> 
</soap:Body></soap:Envelope>

09-08 14:25:25.863: D/gralloc_goldfish(1620): Emulator without GPU emulation detected.
09-08 14:30:00.076: I/Choreographer(1620): Skipped 35 frames!  The application may be doing too much work on its main thread.

Upvotes: 0

Views: 1084

Answers (1)

Chupamobile Evangelist
Chupamobile Evangelist

Reputation: 1116

I think you should follow the steps from your provided website, from the HTTP POST sections.

From what I can understand the request url should be http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit

Also, the POST parameter should be Celsius=77 in your case.

I did a simple test with curl:

curl -d "Celsius=50" http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit

and I got as response:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">122</string>

which seems to be ok.

Try this in your onCreate() method:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);


        BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(
                    "http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit");

            String celsius = "50";
            request.addHeader("Content-Type", "application/x-www-form-urlencoded");

            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("Celsius", celsius));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);

            HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String page = sb.toString();
            // Log.i(tag, page);
            System.out.println(page);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

Where celsius is the value you want to POST.

My output (logcat) looks like this:

06-28 07:57:17.506: I/System.out(1847): <?xml version="1.0" encoding="utf-8"?>
06-28 07:57:17.506: I/System.out(1847): <string xmlns="http://tempuri.org/">122</string>

Upvotes: 1

Related Questions