如何在android中调用这些web服务

时间:2016-07-04 10:40:14

标签: android web-services wcf soap

我正在开发一款使用以下Web Service类型的应用,有人可以帮助我调用此Web Service并从中获取数据吗?请查看以下基本网址,了解webservice

网址

http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants

request format:

{"APIUserName" : "admin" , "Password" : "######"} 

我想要使用此web service但不知道这种web service,那么有人可以帮我调用这个网络服务吗?

4 个答案:

答案 0 :(得分:0)

查看this然后尝试一下

答案 1 :(得分:0)

有很多方法可以使用高级客户端库示例OKhttpVolleyRetrofit从Web服务检索数据,对于您的情况,我认为您可以解析{{ 1}}文件或XML文件并将数据提供给您的应用。

答案 2 :(得分:0)

也许这样的方法应该有效:)

public void postData(String un, String pw) {
    //Create request
    String mynamespace = "http://www.fansplay.com/wsfptb20/FBS.svc";
    String methodName = "GetContestants";

    SoapObject request = new SoapObject(mynamespace, methodName);
    //Property which holds input parameters
    PropertyInfo username = new PropertyInfo();
    //Set Name
    username.setName("APIUserName");
    //Set Value
    username.setValue(un);
    //Set dataType
    username.setType(String.class);
    //Add the property to request object
    request.addProperty(username);

    //Property which holds input parameters
    PropertyInfo password = new PropertyInfo();
    //Set Name
    password.setName("Password");
    //Set Value
    password.setValue(pw);
    //Set dataType
    password.setType(String.class);
    //Add the property to request object
    request.addProperty(password);

    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
    envelope.dotNet = true; //if .NET server
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    String url = "http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants";
    HttpTransportSE androidHttpTransport = new HttpTransportSE(url);

    try {
        //Invoke web service
        String soapAction = "http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants";
        androidHttpTransport.call(soapAction, envelope);
        //Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        //log result to console
        Log.d("myapp", response.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

免责声明:点击此处链接:http://programmerguru.com/android-tutorial/android-webservice-example/

答案 3 :(得分:0)

以下是json解析的一个例子....

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        
    sendRequest();
}

public void sendRequest() {
    String url=getString(R.string.str_url_json);
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    StringRequest stringRequest=new StringRequest(Request.Method.GET,url,this,this);
    requestQueue.add(stringRequest);
}

@Override
public void onErrorResponse(VolleyError error) {
    textView.setText(error.toString());
}

@Override
public void onResponse(String response) {
    int userId,id;
    String title;
    String body;
    Gson gson=new Gson();
    GsonObjects gsonObjects[]=gson.fromJson(response, GsonObjects[].class);
    for(int i=0;i<gsonObjects.length;i++) {
        id = gsonObjects[i].getId();
        userId = gsonObjects[i].getUserId();
        title = gsonObjects[i].getTitle();
        body = gsonObjects[i].getBody();
        listUserId.add(String.valueOf(userId));
        listId.add(String.valueOf(id));
        listTitle.add(title);
        listBody.add(body);
    }    

}

有一个模型类:

public class GsonObjects implements Serializable {
@SerializedName(Constant.JSON_USER_ID)
private int userId;
@SerializedName(Constant.JSON_ID)
private int id;
@SerializedName(Constant.JSON_TITLE)
private String title;
@SerializedName(Constant.JSON_BODY)
private String body;

public int getUserId() {
    return userId;
}

public String getTitle() {
    return title;
}

public int getId() {
    return id;
}

public String getBody() {
    return body;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public void setId(int id) {
    this.id = id;
}

public void setTitle(String title) {
    this.title = title;
}

public void setBody(String body) {
    this.body = body;
}

}