在Android中使用rest来调用Web服务的正确方法?

时间:2015-05-21 17:13:57

标签: android

请建议任何解决方案? 我是android的新手。我在调用Web服务时遇到错误

W/System.err﹕ java.io.IOException: Method Not Allowed

这是我的活动,它调用一个Web服务(方法),它接受一个字符串参数并提供输出。如果有人使用asynctask发布代码片段,我很高兴因为它是在android中调用服务的最佳方式....

public class closingBalance extends ActionBarActivity {
    protected final String NAMESPACE = "http://xxxxx/";

    protected final String METHOD_NAME = "getReportDetails";
    protected final String URL = "http://xxxxx?wsdl?shop_num=12345";

    HttpClient client = new DefaultHttpClient();
    try {
        HttpResponse res = client.execute(new HttpGet(URL));
        StatusLine line  = res.getStatusLine();
        if(line.getStatusCode() == HttpStatus.SC_OK){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            res.getEntity().writeTo(out);
            String response = out.toString();
            System.out.println(response);
            out.close();

        }else{
            res.getEntity().getContent().close();
            throw  new IOException(line.getReasonPhrase());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

我建议你研究一下Volley工具对你非常有用。

使用Volley传输网络数据非常简单

https://developer.android.com/training/volley/index.html

答案 1 :(得分:0)

考虑使用Retrofit库。

Here是AsyncTask,Volley和Retrofit之间的比较