简体   繁体   English

完全丢失尝试致电Web服务

[英]Completely Lost Trying to call web service

I have asked variations of this question over the past couple of days. 在过去的几天里,我曾问过这个问题的各种变化。 I am not understanding or making the proper connections etc. so I am getting really frustrated not understanding which path I should be on. 我不了解或未建立正确的连接等。因此,我不了解应该走哪条路就感到非常沮丧。

I have developed a XAMLX WCF workflow service and my windows clients interact with it just fine. 我已经开发了XAMLX WCF工作流服务,并且我的Windows客户端与之交互就很好。 I am new to android and java and thus basically a NOOB. 我是android和java的新手,因此基本上是NOOB。 My service uses BasicHttpBinding and expects a couple of string values (uid & pwd) in the request. 我的服务使用BasicHttpBinding,并且在请求中需要几个字符串值(uid和pwd)。 The response is simply a string. 响应只是一个字符串。

Now on the android I am totally lost. 现在在android上我完全迷路了。 I have read to use the AsyncTask, I've heard it has major issues and to use RoboSpice. 我已阅读过使用AsyncTask的信息,听说它存在重大问题并使用RoboSpice。 Looking at RoboSpice it looks very solid but very stiff learning curve. 看着RoboSpice,它看起来很扎实,但是学习曲线却很僵硬。 I've heard for the stiff learning curve to use LoopJ as it will be easier when starting out. 我听说过要使用LoopJ进行严格的学习,因为开始时会更容易。 Throw in there the people who want you to use JSON and yeah. 把想要您使用JSON和yeah的人扔在那里。 I'm definitely lost. 我肯定迷路了。

So I have been pulled in a lot of different directions. 所以我被拉到许多不同的方向。 I know that I need it to be Async so it doesn't lock the ui thread and I would like to get to a solid robust solution but for now a baby step of just interacting with the service even synchronously would be encouraging. 我知道我需要它是异步的,因此它不会锁定ui线程,并且我想寻求一个可靠的可靠解决方案,但就目前而言,即使与服务进行交互的简单步骤也会令人鼓舞。

Here are my basics: Send a request to the service. 这是我的基本知识:向服务发送请求。 Have the application pause/loop etc. A progress bar would be wonderful but a simple loop that doesn't hang would be just fine for now. 让应用程序暂停/循环等。进度条会很不错,但是暂时不挂起的简单循环就可以了。 RX the response and continue processing. 接收响应并继续处理。

This is so simple for me in .NET I thought it would be simple. 在.NET中,这对我来说是如此简单,以至于我认为这很简单。 Just goes to show that pride cometh before the fall. 只是表明骄傲在秋天之前就降临了。

TIA JB TIA JB


Direct Question 直接问题

As has been commented there are several implied questions in my OP. 如前所述,我的操作中有几个隐含的问题。 That is because I don't know what to ask. 那是因为我不知道该问些什么。 So let's go with AsynchTask for now. 因此,现在让我们使用AsynchTask。 Let's call that my baby step. 我们称其为我的宝贝。 Here are my questions: HOW do I associate values to populate the request? 这是我的问题: 如何关联值以填充请求? In .NET I would create an object that matches what the request expects. 在.NET中,我将创建一个与请求期望匹配的对象。 So my object would have a UserName property and Password property. 因此,我的对象将具有UserName属性和Password属性。 I would assign them their values like obj.UserName = "Joe"; 我将为其分配值,如obj.UserName =“ Joe”; I would then call the service operation and pass it obj. 然后,我将调用服务操作并将其传递给obj。

HOW do I ensure I am using the proper libraries? 如何确保我使用正确的库? I have read that AsyncTask uses the Apache Http Client library. 我已经读过AsyncTask使用Apache Http Client库。 OK I google that to get the jar's and apparently it's end of life. 好吧,我用谷歌搜索得到的罐子,显然这是生命的尽头。 But still available so I download the latest and put it in my lib folder....but I didn't have to because I can import org.apache.http.HttpResponse without it so which version am I using then and should I use an older one or the latest. 但是仍然可用,因此我下载了最新版本并将其放在我的lib文件夹中。...但我不必这样做,因为没有它我就可以导入org.apache.http.HttpResponse,所以我当时应该使用哪个版本,应该使用一个较旧的或最新的。

**Finally how do I get the app to pause because this code does something (although it never shows in the logs of my service machine) and while it is off doing something my code continues to execute and crashes when it reaches the point where it needs the data from the service.....but it's not there. **最后,如何使应用程序暂停,因为该代码可以执行某些操作(尽管它永远不会显示在服务机的日志中),而当它关闭时,我的代码会继续执行并在到达该位置时崩溃需要来自服务的数据.....但是不存在。 A progress bar would be the bees knees. 进度条将是蜜蜂的膝盖。

Here is my code for the implementation: 这是我的实现代码:

public class AsyncHttpRequestManager extends AsyncTask<String, Boolean, String>
{
private static final String serviceUrl = "http://www.serviceurl.com/service.xamlx";

@Override
protected String doInBackground(String... params) 
{

    try 
    {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPut putRequest = new HttpPut(serviceUrl);
        putRequest.setHeader("Content-type", "text/plain");
        putRequest.setHeader("Accept", "text/plain");
            putRequest.setEntity(new StringEntity(params[0])); //This one is connected to my first question. How do I get the proper associations for the operations contract.
            HttpResponse response = client.execute(putRequest);
            InputStream content = response.getEntity().getContent();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
        String result = "";
        String s = "";
        while ((s = buffer.readLine()) != null) 
        {
            result += s;
        }
        return result;

    }
    catch (UnsupportedEncodingException e) 
    {
        Log.e("AsyncOperationFailed", e.getMessage());
        e.printStackTrace();
    } catch (ClientProtocolException e) 
    {
        Log.e("AsyncOperationFailed", e.getMessage());
        e.printStackTrace();
    } catch (IOException e) 
    {
        Log.e("AsyncOperationFailed", e.getMessage());
        e.printStackTrace();
    }
    return null;

}

protected void onPostExecute(String result) 
{
    Log.d("MESSAGE", result);
}

} }

I call this like this: 我这样称呼它:

new AsyncHttpRequestManager().execute("joe","asdf");

If more info is needed I will gladly post. 如果需要更多信息,我将很乐意发布。

You should ask one question at a time, it's how SO is designed, and curated, to work. 您应该一次提出一个问题,那就是SO是如何设计和管理的。

For HTTP authentication 对于HTTP身份验证

http://developer.android.com/reference/org/apache/http/auth/UsernamePasswordCredentials.html http://developer.android.com/reference/org/apache/http/auth/UsernamePasswordCredentials.html

Eg 例如

HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(new AuthScope("myhost", 80, AuthScope.ANY_REALM), defaultcreds);

Asynctask does not require any external libraries it's part of the Android platform. Asynctask不需要任何外部库,它是Android平台的一部分。 Don't get confused about HTTPClient and Asynctask. 不要对HTTPClient和Asynctask感到困惑。 Asyntask is a generic class intended to call background processing on a separate thread from the main UI thread to avoid blocking. Asyntask是一个通用类,旨在在与主UI线程不同的线程上调用后台处理以避免阻塞。 It just so happens that you are considering using an Asynctask to process your web request but they are not linked in any way. 碰巧的是,您正在考虑使用Asynctask处理您的Web请求,但它们没有以任何方式链接。

Your question about "how to return data to my app" is best posed separately but, in essence, you're thinking sequentially. 关于“如何将数据返回到我的应用程序”的问题最好单独提出,但从本质上讲,您是在顺序考虑。 Your app should not pause. 您的应用不应暂停。 There are many methods, progress dialogs being one, to properly handle this. 有许多方法可以正确处理此问题,进度对话框是其中一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM