0

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. I am new to android and java and thus basically a NOOB. My service uses BasicHttpBinding and expects a couple of string values (uid & pwd) in the request. The response is simply a string.

Now on the android I am totally lost. I have read to use the AsyncTask, I've heard it has major issues and to use RoboSpice. Looking at RoboSpice it looks very solid but very stiff learning curve. I've heard for the stiff learning curve to use LoopJ as it will be easier when starting out. Throw in there the people who want you to use JSON and 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.

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. Just goes to show that pride cometh before the fall.

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. 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. So my object would have a UserName property and Password property. I would assign them their values like obj.UserName = "Joe"; I would then call the service operation and pass it obj.

HOW do I ensure I am using the proper libraries? I have read that AsyncTask uses the Apache Http Client library. 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.

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

GPGVM
  • 5,515
  • 10
  • 56
  • 97
  • Network operations should be done on a thread otherthen ui thread. So using asynctask is a possibility. I mostly use own created thread to handdlle network operations and use broadcast receiverbto update ui. – Tobrun Jan 01 '13 at 19:19
  • "I have read to use the AsyncTask, I've heard it has major issues" What major issues? AsyncTask can be tricky if you're trying to update the UI from it but using it for HTTP binds, then utilising a callback or broadcast to load the UI is standard practice. Aside from that, what is your *concrete* question. There seem to be many implied questions in your post. – Simon Jan 01 '13 at 19:22
  • 1
    I've just read your other question. My personal advice is to try to do everything "off the shelf". Only resort to 3rd party libraries when the basic framework does not enable you to accomplish something. This question may help http://stackoverflow.com/questions/3505930/make-an-http-request-with-android – Simon Jan 01 '13 at 19:42

1 Answers1

0

You should ask one question at a time, it's how SO is designed, and curated, to work.

For HTTP authentication

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

E.g.

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. Don't get confused about HTTPClient and Asynctask. Asyntask is a generic class intended to call background processing on a separate thread from the main UI thread to avoid blocking. It just so happens that you are considering using an Asynctask to process your web request but they are not linked in any way.

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.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • Fair enough one question. WHERE?????? do you pass in the values needed by the service request contract. For example in my OP edit what line / where are my request parms sent so the service knows what data to retrieve and send back?? – GPGVM Jan 01 '13 at 22:11