2

I'm trying to code a simple android app to help me sign-in to workouts faster and easily.

i did a POC with python:

import httplib
conn = httplib.HTTPConnection("whitecity.wodsignup.com")
conn.request("GET","/act.php?act=joinWOD&time=1422712800&type=wod&user=791723491%2CGil+Alin")
res = conn.getresponse()
print res.status, res.reason

the result i get from the server is 200 OK , and it works fine (works = signs me up to the requested workout)

Now the tricky part - i created the following app:

package com.example.httpreqsample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    TextView etResponse;
    TextView tvIsConnected;
    Button go;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get reference to the views
        etResponse = (TextView) findViewById(R.id.etResponse);
        tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
        go = (Button) findViewById(R.id.go);

        go.setOnClickListener(this);

        // check if you are connected or not
        if(isConnected()){
            tvIsConnected.setBackgroundColor(0xFF00CC00);
            tvIsConnected.setText("You are conncted");
        }
        else{
            tvIsConnected.setText("You are NOT conncted");
            tvIsConnected.setBackgroundColor(Color.RED);
        }

        // show response on the EditText etResponse 

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        // calling the GET via Button
        //etResponse.setText(GET("whitecity.wodsignup.com/act.php?act=joinWOD&time=1422412800&type=wod&user=791723491%2CGil+Alin"));
    }

    public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

    // convert inputstream to String
    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

    // check network connection
    public boolean isConnected(){
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) 
                return true;
            else
                return false;   
    }

    @Override
    public void onClick(View v) {

        etResponse.setText(GET("http://whitecity.wodsignup.com/act.php?act=joinWOD&time=1422412800&type=wod&user=791723491%2CGil+Alin"));
    }

}

i am able to communicate with the server, and receive the following results:

{"code":200,"message":"Removed from lesson"}

or

{"code":201,"message":"All good"} 

alternating between the two, each time i send the GET request.

The problem is that the Android solution doesn't seem to actually work. it sends and receives data from the site but doesn't really sign me up like the Python solution

any idea why the two approaches do not produce the same result?

Gil
  • 21
  • 2
  • I was just watching the beauty of python language, 4 lines of code and you are done , whereas JAVA , hmmmmm I fall a short of fingers to count the number of lines. – ZdaR Jan 30 '15 at 17:19

1 Answers1

0

I think both solutions work ok. It just site script that works like that.

Requests switch between {"code":200,"message":"Removed from lesson"} and {"code":201,"message":"All good"} probably toggling between removing and subscribing on lesson

Just check it in any browser by going to http://whitecity.wodsignup.com/act.php?act=joinWOD&time=1422412800&type=wod&user=791723491%2CGil+Alin and refreshing site few times

ZqBany
  • 201
  • 1
  • 5
  • Thanks, but the problem is this: the sign up site uses a chart that shows icons of the all the people who signed in in each leason. When i said that the pyton solution worked, I meant that it updated my icon in the desired leason. While the JAVA solution only returns resulta as if all is fine, but my icon doesn't really apear in the desired leason. – Gil Jan 30 '15 at 19:30
  • @Gil Does python solution still works? Maybe it's just because it was first GET request that it worked – ZqBany Jan 30 '15 at 19:40
  • The python is even able to sign and unsign me every time – Gil Jan 30 '15 at 20:31
  • @Gil What happens when you paste Get request in browser? Are there any differences between computer browser and android device browser? – ZqBany Jan 30 '15 at 20:41
  • Actually the behaviour of the android device is exactly the same as pasting the GET request in the browser! In both of them ibgetbthe response 200 or 201, but nothing really happens.. – Gil Jan 30 '15 at 20:58
  • @Gil my only idea is installing web proxy [Fiddler](http://www.telerik.com/fiddler), connecting to it [from python](http://stackoverflow.com/questions/1450132/proxy-with-urllib2) by setting Fiddler IP address and port (by default, 127.0.0.1 and 8888) and inspecting what exact get request it sends (content, headers etc.) – ZqBany Jan 30 '15 at 22:56