0

I am working on an Android application which allow user to write feedback and then the articleID, ticketID and ticketnumber of the user will be generated by the server and be returned to the user.

There are two activity in this application. MainActivity allow the user to enter their details and a submit button will start Process activity that will send the details to the server and shows the articleID, ticketID and ticketnumber that are returned.

The problem is, it will only work once every time the application is started. For example, a user open the application and enter his details, the submit button is pressed and the corresponding articleID, ticketID and ticketnumber are returned. Then he tries to submit a second one by returning to the previous activity. He enters his detail again and press submit. This time, null is returned.

Images of example is shown here https://i.stack.imgur.com/unf2N.jpg

However, the application works again if it is quitted and the RAM is cleared.

I tried to use this method here to restart the application but still it did not work.

Below is the kSoap code in the Process activity.

public class Process extends Activity{

private String URL = " /*WORKING URL*/";
private String NAMESPACE = "/*WORKING URL*/";
private String soapUsername = "/*WORKING USERNAME*/";
private String soapPass = "/*WORKING PASSWORD*/";
private String METHOD_NAME = "TicketCreate";
private String SOAP_ACTION = "/*WORKING URL*/";
private Handler handler = new Handler();
private Thread thread;
TextView emailT, subjectT, complaintT, responseT, nameT;
String email, subject, complaint, name;
String articleid , ticketid ,ticketnumber;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.process);
    webservice();

    nameT = (TextView) findViewById(R.id.name);
    emailT = (TextView) findViewById(R.id.email);
    subjectT = (TextView) findViewById(R.id.subject);
    complaintT = (TextView) findViewById(R.id.complaint);
    responseT = (TextView) findViewById(R.id.responsevalue);

    Intent i = getIntent();
    // Receiving the Data
    name = i.getStringExtra("name");
    email = i.getStringExtra("email");
    subject = i.getStringExtra("subject");
    complaint = i.getStringExtra("complaint");

    // Displaying Received data
    nameT.setText(name);
    emailT.setText(email);
    subjectT.setText(subject);
    complaintT.setText(complaint);

    Button fin= (Button)findViewById(R.id.finish);
    fin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}


public void webservice(){
    thread = new Thread(){
        public void run(){
            try 
            {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                // Set all input params   
                request.addProperty("UserLogin", soapUsername);
                request.addProperty("Password", soapPass);
                Hashtable<String, String> ticket = new Hashtable<String, String>();
                ticket.put("Title", subject);
                ticket.put("CustomerUser", email);
                ticket.put("CustomerID", "soapwebnologin");
                ticket.put("QueueID", "3");
                ticket.put("State", "new");
                ticket.put("PriorityID", "1");
                ticket.put("Lock", "unlock");
                ticket.put("OwnerID", "1");
                request.addProperty("Ticket", ticket);

                Hashtable<String, String> article = new Hashtable<String, String>();
                article.put("Subject", subject);
                article.put("Body", complaint);
                article.put("ContentType", "text/plain; charset=utf8");
                request.addProperty("Article", article);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                new MarshalHashtable().register(envelope); 
                envelope.dotNet = true;
                envelope.bodyOut = request; 

                String check = checkSSL(URL);

                if(check == "SSL"){
                    KeepAliveHttpsTransportSE httpT = new  KeepAliveHttpsTransportSE("/*WORKING URL*/", /*WORKING PORT*/, METHOD_NAME, 15000);

                    httpT.debug = true;

                    httpT.call(SOAP_ACTION, envelope);

                     KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
                     articleid = ks.getProperty(0).toString();
                     ticketid = ks.getProperty(1).toString();
                     ticketnumber = ks.getProperty(2).toString();

                    Log.e("dump Request: " ,httpT.requestDump);
                    Log.e("dump response: " ,httpT.responseDump);
                    Log.e("object response : ", ks.toString());
                }
                    else{
                    HttpTransportSE httpT = new HttpTransportSE(URL);

                    httpT.debug = true;

                    httpT.call(SOAP_ACTION, envelope);

                     KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
                     articleid = ks.getProperty(0).toString();
                     ticketid = ks.getProperty(1).toString();
                     ticketnumber = ks.getProperty(2).toString();

                    Log.e("dump Request: " ,httpT.requestDump);
                    Log.e("dump response: " ,httpT.responseDump);
                    Log.e("object response : ", ks.toString());
                    }


            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            handler.post(createUI);
        }
    };

    thread.start();

}


final Runnable createUI = new Runnable() {
    public void run(){
        responseT.setText("Your ticket id =" + ticketid+ " Article id ="+ articleid+" TICKET NUMBER ="+ ticketnumber);
    }
};

protected String checkSSL(String url){
    String https = url.substring(0, 4);
    if(https == "https"){
        return "SSL";
    }
    else{
        return "noSSL";
    }
}
 }

EDIT: When I rotate the the screen, it requested another ticket from the server and it actually works. I am so confused.

Community
  • 1
  • 1
Joel Quek
  • 33
  • 4

1 Answers1

0

Apparently it is a bug which can be fixed by adding this line:

System.setProperty("http.keepAlive", "false");

I ran into an Android OS bug recently that is pretty harsh related to HTTPS connections. Basically, what happens is this:

  1. You want to setup a connection between the phone and a server, and you need to control both the input and the output. As a result, you use URL.openConnection(), with setDoInput() and setDoOutput() set to true:

URL url = new URL("https://blahblahblah.com"); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true);

At some point you use both conn.getOutputStream() to write to the stream, then conn.getInputStream() to get the response.

  1. You're doing an HTTPS connection. Some people report this happening on normal HTTP, but I've only seen it happen on HTTPS.

  2. The first request goes through fine and dandy.

  3. The second time you try to make the request, the connection doesn't send any data out and doesn't receive any data; it looks like it happens instantly. If you cast to an HttpURLConnection, conn.getResponseCode() returns -1 instead of anything meaningful.

In other words, every other request, the request fails outright. This is a noted bug in Android, but it isn't fixed yet in any released versions. Even when it's fixed, you'll still have to deal with this on older versions of Android.

There are a few workarounds. The first is to simply not use URLConnection; if you can find some way around it, avoid it. The second is to repeatedly make the same request until it works; it's a little too much of a hack for my tastes, but it'll work.

Then there's the third workaround, which I do not claim to understand why it fixes the issue but it does. Just set this setting when your application begins:

System.setProperty("http.keepAlive", "false");

Unfortunately this has some drawbacks (keep-alive is a good thing normally), but in comparison to mysteriously failed requests I'll ditch it.

Source: http://daniel-codes.blogspot.com/2010_07_01_archive.html

And regarding the problems with orientation change, read here Change screen orientation in Android without reloading the activity

Community
  • 1
  • 1
Joel Quek
  • 33
  • 4