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?