0

AndroidRESTfulコンシューマーを使用してWebサービスから単純な出力を取得する際に問題が発生しました。以下は私のコードと私の出力です。ただし、このWebサービスから返される値を取得できないようです。http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

誰かがSOAPメッセージでなぜそれが言うのか理解するのを手伝ってくれるなら私は大いに感謝します

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

私のコードはコンパイルされて正常に実行されます

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();
            }
        }
    }
}
}

私の出力

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.
4

1 に答える 1

1

HTTP POST提供されたWebサイトのセクションの手順に従う必要があると思います。

私が理解できることから、リクエストのURLはhttp://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit

Celsius=77また、POSTパラメータはあなたの場合にあるべきです。

私はcurlで簡単なテストを行いました:

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

そして私は応答として得ました:

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

大丈夫そうです。

あなたの方法でこれを試してくださいonCreate()

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();
                }
            }

POSTする値はどこcelsiusにありますか。

私の出力(logcat)は次のようになります:

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>
于 2012-09-08T20:01:05.847 に答える