Monday, October 31, 2011

SUSE : Eclipse INDIGO Subversion - Google.Code


http://pantestmb.blogspot.com/2011/08/eclipse-subversion-subversive.html












SUSE : Eclipse Indigo - Android Development Plugin


http://pantestmb.blogspot.com/2011/08/eclipse-indigo-with-android-development.html



  1. Start Eclipse, then select Help > Install New Software....
  2. Click Add, in the top-right corner.
  3. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location:
    https://dl-ssl.google.com/android/eclipse/
  4. Click OK Note: If you have trouble acquiring the plugin, try using "http" in the Location URL, instead of "https" (https is preferred for security reasons).
  5. In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
  6. In the next window, you'll see a list of the tools to be downloaded. Click Next.
  7. Read and accept the license agreements, then click Finish. Note: If you get a security warning saying that the authenticity or validity of the software can't be established, click OK.
  8. When the installation completes, restart Eclipse.












Friday, October 28, 2011

Step 7 : POST data from JSON Android Client to WCF REST WebService

POST JSON data from an Android Client to a WCF RESTful WebService

The full Android.java source code is on Google Code :
http://code.google.com/p/jtelmon/source/browse/trunk/AndroidViews/src/net/learn2develop/AndroidViews/

The full Eclipse Indigo 3.7 - Android  project zipped on Google Docs :
https://docs.google.com/leaf?id=0BzKVfKe--t_cMDU5OTU5MDQtNDQzOC00YjE3LTlhODgtYjE1ZDhmZWFmZjc1&hl=en_GB


All files of the WCF RESTful Web Service are also on Google Code :
http://code.google.com/p/jtelmon/source/browse/trunk/AndroidViews/RestService

The full Visual Web Developer project -  VWD  Express 2010 also on Google Docs :
https://docs.google.com/leaf?id=0BzKVfKe--t_cODIwZWE2M2QtZTUwMy00Yjg1LTljMjUtYTA3MGI4YmM5OGZm&hl=en_GB

First we tried to post XML formatted data to a WCF Web Service with FIDDLER ( step 5 ) :
http://pantestmb.blogspot.com/2011/10/pass-multiple-body-parameters-wcf-rest.html

Then tried to post JSON data to a WCF Web Service with FIDDLER ( step 6 ) :
http://pantestmb.blogspot.com/2011/10/wcf-rest-fiddler-json-request-body.html

Now , knowing that the WCF Web Service responds OK with both XML and JSON ,
we can take our simple sample project to the ANDROID Client Test :


http://code.google.com/p/jtelmon/source/browse/trunk/AndroidViews/src/net/learn2develop/AndroidViews/SavePerson.java

package net.learn2develop.AndroidViews;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
// import org.json.JSONObject;
import org.json.JSONStringer;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class SavePerson extends Activity {

private final static String SERVICE_URI = "http://192.168.61.3/RestServicePost/RestServiceImpl.svc";

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);
       // String plate = new String("test");
       // POST request to
            HttpPost request = new HttpPost(SERVICE_URI + "/json/adduser");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
         
            String not = new String(" ");
            try {
            // Build JSON string
            JSONStringer vehicle = new JSONStringer()
                .object()
                    .key("rData")
                        .object()
                            .key("details").value("bar|bob|b@h.us|why")
                        .endObject()
                    .endObject();
         
            StringEntity entity = new StringEntity(vehicle.toString());
         
            Toast.makeText(this, vehicle.toString() + "\n", Toast.LENGTH_LONG).show() ;
         
            request.setEntity(entity);
         
            // Send request to WCF service
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            // Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
            Toast.makeText(this, response.getStatusLine().getStatusCode() + "\n", Toast.LENGTH_LONG).show() ;
         
            }catch (Exception e) {
            not = "NOT ";
            }
         
            Toast.makeText(this, not + " OK ! " + "\n", Toast.LENGTH_LONG).show() ;
         
         

}  
}


http://code.google.com/p/jtelmon/source/browse/trunk/AndroidViews/RestService/IRestServiceImpl.cs


      [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/adduser")]
        PersonData AddJsonUser(RequestData rData);

In our simple example we create a JSONStringer object with "rData" key and "details" sub-key ;
      "rData" is the variable of the function "AddJsonUser" ;
"details" is the single property of the object "RequestData" :
 http://code.google.com/p/jtelmon/source/browse/trunk/AndroidViews/RestService/RequestData.cs


    [DataContract(Namespace = "")]
    public class RequestData
    {
        [DataMember]
        public string details { get; set; }
    }





The string  "bar|bob|b@h.us|why" of the JSONStringer is hard-coded to simplify things ;
After being parsed - it ends up in the SQLS table "Users" : 





http://code.google.com/p/jtelmon/source/browse/trunk/AndroidViews/RestService/RestServiceImpl.svc.cs
AddUser is the function triggered by the POST WCF Webservice
http://192.168.61.3/RestServicePost/RestServiceImpl.svc/json/adduser
with a JSON request body -  JSONStringer vehicle : 


        public PersonData AddUser(RequestData rData)
        {
            bool returnBool = false;

            var data = rData.details.Split('|');
            var response = new PersonData
            {
                Name = data[0],
                User = data[1],
                Email = data[2],
                Password = data[3]
            };

            SqlConnection dbConn = new SqlConnection(connStr);
            string sqlStr = "INSERT INTO users(username,name,email,password)
                          values('" + data[0] + "', '" + data[1] + "', '" + data[2] + "', '" + data[3] + "');";
            SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);
            try
            {
                dbConn.Open();
                if (dbCommand.ExecuteNonQuery() != 0)
                {
                    returnBool = true;
                }
                dbConn.Close();
            }
            catch
            {
                returnBool = false;
            }
            return response;
        }

        public PersonData AddJsonUser(RequestData rData)
        {
            return AddUser(rData);
        }



~ ~ ~


the SQL Server Database has only one table - Users :

CREATE TABLE [dbo].[Users](
[UserName] [varchar](100) NOT NULL,
[Name] [varchar](100) NOT NULL,
[EMail] [varchar](100) NOT NULL,
[Password] [varchar](100) NOT NULL,


Registering ASP.NET on IIS

Registering ASP.NET on IIS after installing the .NET Framework




aspnet_regiis.exe, it is located under %WindowsDir%\Microsoft.NET\Framework\vx.y.zzzz\ and should be called with the -i parameter: aspnet_regiis.exe -i








run aspnet_regiis.exe -i in VS 2010 command prompt.
http://pantestmb.blogspot.com/2011/10/svc-integrated-bad-module.html

Wednesday, October 26, 2011

Step 6 : WCF REST & FIDDLER JSON - request body


WCF REST * FIDDLER JSON - request body , headers and content type : 



  [DataContract(Namespace = "")]
    public class PersonData
    {
        [DataMember]
        public string Name { get; set; }


        [DataMember]
        public string User { get; set; }


        [DataMember]
        public string Email { get; set; }


        [DataMember]
        public string Password { get; set; }
    }




    [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "jsontestpersondatapost")]
        PersonData JsonTestPersonDataPost(PersonData pd);





       public PersonData TestPersonDataPost(PersonData pd)
        {
            var response = new PersonData
            {
                Name = pd.Name,
                User = pd.User,
                Email = pd.Email,
                Password = pd.Password
            };
            
            return response;
            // return pd;
        }


        public PersonData JsonTestPersonDataPost(PersonData pd)
        { 
            return TestPersonDataPost(pd);
        }









POST http://192.168.61.3/RestServicePost/RestServiceImpl.svc/jsontestpersondatapost HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: 192.168.61.3
Content-Length: 86


{ "pd" : { "Email":"bo@se.us" , "Name":"amab" , "Password":"egti" , "User":"brac" } } 










HTTP/1.1 200 OK
Content-Length: 99
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Wed, 26 Oct 2011 18:31:54 GMT


{"JsonTestPersonDataPostResult":{"Email":"bo@se.us","Name":"amab","Password":"egti","User":"brac"}}










Tuesday, October 25, 2011

POST JSON from Android using HttpClient




POST JSON from Android using HttpClient




Step 5 : Pass multiple body parameters in wcf rest



How pass multiple body parameters in wcf rest

using webinvoke method(Post or PUT)








Even If you have blank namespace  [DataContract(Namespace = "")] you have to put 
xmlns="" in the request body ... ( see the pictures below )
Whithout xmlns="" in the request body , problems can appear 






  [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "testpersondatapost")]
        PersonData TestPersonDataPost(PersonData pd);


  [DataContract(Namespace = "")]
    public class PersonData
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string User { get; set; }

        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string Password { get; set; }
    }


       public PersonData TestPersonDataPost(PersonData pd)
        {
            var response = new PersonData
            {
                Name = pd.Name,
                User = pd.User,
                Email = pd.Email,
                Password = pd.Password
            };
            
            return response;
            // return pd;
        }
























Friday, October 21, 2011

Step 4 : From SOAP to WCF-RESTful-DELETE


SOAP :



[WebMethod(Description = "Method to Delete User")]
public bool DeleteUser(string username)
{
   bool returnBool = false;
   SqlConnection dbConn = new SqlConnection(connStr);
   string sqlStr = "DELETE FROM users where username = '" + username +"';";
   SqlCommand dbCommand = new SqlCommand(sqlStr,dbConn);
try
{
   dbConn.Open();
   if (dbCommand.ExecuteNonQuery()!=0)
   {
      returnBool=true;
   }
}
catch
{
   returnBool=false;
}
   dbConn.Close();
   return returnBool;
}




WCF REST :


        [OperationContract]
        [WebInvoke(Method = "DELETE",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "usrdel/{user}")]
        bool DeletePerson(string user);


        public bool DeletePerson(string user)
        {
            bool returnBool = false;
            SqlConnection dbConn = new SqlConnection(connStr);
            string sqlStr = "DELETE FROM users where username = '" + user + "';";
            SqlCommand dbCommand = new SqlCommand(sqlStr, dbConn);
            try
            {
                dbConn.Open();
                if (dbCommand.ExecuteNonQuery() != 0)
                {
                    returnBool = true;
                }
            }
            catch
            {
                returnBool = false;
            }
            dbConn.Close();
            return returnBool;
        }



Step 3 : From SOAP to WCF-RESTful-GET



Step 3 : From SOAP to WCF-RESTful-GET

Google Docs PDF :
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0BzKVfKe--t_cZjM4YWU2NWMtZDQ3Yy00ZWZmLTgyYWYtZDQyM2EzN2EzYjVk&hl=en_GB


SOAP :
[WebMethod]
public List GetPersonList()
{
   SqlConnection dbConn = new SqlConnection(connStr);
   dbConn.Open();
   string sqlSelect = "select * from users ";
   SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
   DataTable dt = new DataTable();
   SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
   da.Fill(dt);
   dbConn.Close();
   List myList = new List();
   foreach (DataRow row in dt.Rows)
   {
      Person target = new Person();
      target.Name = row["Name"].ToString();
      target.UserName = row["UserName"].ToString();
      target.EMail = row["EMail"].ToString();
      target.Password = row["Password"].ToString();
      myList.Add(target);
   }
   return myList;
}



public class Person
{
private string _name = string.Empty;
private string _user_name = string.Empty;
private string _eMaiL = string.Empty;
private string _password = string.Empty;
public Person() {}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Password
{
get { return _password; }
set { _password = value; }
}
public string UserName
{
get { return _user_name; }
set { _user_name = value; }
}
public string EMail
{
get { return _eMaiL; }
set { _eMaiL = value; }
}
}


WCF REST : getAllPersons

[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/getallpersons")]
PersonData[] getAllPersons();

[DataContract]
public class PersonData
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string User { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Password { get; set; }
}


public PersonData[] getAllPersons()
{
SqlConnection dbConn = new SqlConnection(connStr);
dbConn.Open();
string sqlSelect = "select * from users ";
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
DataTable dt = new DataTable();
SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
da.Fill(dt);
dbConn.Close();
List<PersonData> list = new List<PersonData>();
foreach (DataRow row in dt.Rows)
{
// Person target = Activator.CreateInstance();
PersonData target = new PersonData();
target.Name = row["Name"].ToString();
target.User = row["UserName"].ToString();
target.Email = row["EMail"].ToString();
target.Password = row["Password"].ToString();
// DataColumnAttribute.Bind(row,target);
list.Add(target);
}
return list.ToArray();
}





http://192.168.61.3/RestServicePost/RestServiceImpl.svc/xml/getallpersons





VWD 2010 Express - full project : 
https://docs.google.com/leaf?id=0BzKVfKe--t_cMmUxYTlkY2UtZmIzOC00YjhjLWExN2MtNDk3MWFhNDQ4ZWY0&hl=en_GB






http://192.168.61.3/RestServicePost/RestServiceImpl.svc/json/getallpersons

More about JSON part of the RESTful WebService on :

Step 8 : WCF RESTful GET JSON List with FIDDLER