views:

107

answers:

1

is stringbuilder is same in android java and c# ??

im using stringbuilder in c#(REST Webservice).. how can i use with the same functionality in Java?

or im using stringentity in java.wat is the equivalent in c#(REST Webservice)?

HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit

HttpResponse response;
JSONObject json = new JSONObject();
String URL ="url";
try{

     HttpPost post = new HttpPost(URL);
     json.put("CNo",112);
     json.put("CName",name);

     StringEntity se = new StringEntity(json.toString());
     se.setContentType("application/json; charset=utf-8");
     se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8"));

     post.setHeader("Accept","application/json");
     post.setHeader("Content-type","application/json; charset=utf-8");
     String ss= post.toString();
     response = client.execute(post);

this is for post()

and in webservice im implementing by

public bool CreateCustomer(StringBuilder strObj)
{
    // JavaScriptSerializer js = new JavaScriptSerializer();
    //  Customer custObj = js.Deserialize<Customer>(strObj.ToString());
    //  strObj.ToString();
    bool Inserted = false;
    String connString = ConfigurationManager.ConnectionStrings["connWebOrdering"].ConnectionString;
    SqlConnection Conn = new SqlConnection(connString);
    try
    {
        SqlCommand cmd = new SqlCommand("insert into cust(obj) values('" + strObj + "')", Conn);
        Conn.Open();
        int rowsaffected = cmd.ExecuteNonQuery();
        if (rowsaffected == 1)
        {
            Inserted = true;
        }
    }
    catch (Exception)
    { }
    finally
    {
        Conn.Close();
    }

here the inserted data in database is "blank"... the response comes to as "OK..Status 200"

+3  A: 

Why are you trying to pass the entire object?

You can't pass a stringbuilder object from one programming paradigm to another - i.e., even if you serialise the StringBuilder object in Java, I'm suspecting you won't be able to suck it into a C# StringBuilder (unless a serialised StringBuilder in java is simply a string).

You'll have to pass the string contained within the stringbuilder object as a string. The C# stringbuilder can then construct itself using the provided string as it's initial state.

This goes for any object -- it will be easier and simpler to pass through the simple item rather than an entire object, so cast everything down to it's most basic (primitive) type (string, int, if it's XML, serialise it to a string: the remote end can deserialise), etc.

Chris J
hi chris, this is the service i wrote public bool CreateCustomer(StringBuilder strObj) { //here i insert into db//} but this same function the android could not recognise because of the stringbuilder.. what can i do for this?? the only possible thing in android is to post using stringentity.. what is the equivalent of it in c#?? and im using json format
ganesh
i have implemented the same thing of converting the json to string format and sending to the service.. but still the data is not received
ganesh
Can you update your question including: (1) an example of how you're calling the method from Java (2) how the service is written (which you've included in your comment, but it's better in the question) and (3) say what happens when you cast to a basic string type (I don't know what stringentitiy is) and (4) what errors (if any) you're seeing when you call the service.
Chris J
hi chris i have updated..
ganesh
Well one thing I notice is that you're ignoring any exception: you've got an empty 'catch' block, so the service code isn't going to tell you if it does fall over.
Chris J
ok.. i have modified... yet the problem remains the same
ganesh
finally.. i want to send the string entity object as string from android client to .net rest service..
ganesh
Modified in what sense? What are you doing with the exception? Also you're passing in a StringBuilder object ot the method -- as mentioned in one of my comments above: pass everything around as strings. A StringBuilder is fairly heavyweight if all you're going to do is take a string and use it without modifying it. You might want to stick a Wireshark trace between the server and the client -- confirm that what you're expecting to see passed between client and server is actually being passed: you need to narrow down whether it's the client code or the server code at fault.
Chris J
in catch i print the trace.. ya the object which is sent from android(string) is not able to recognise in .net rest service.. and in database the data is blank.. even if i implement as string in my service it shows as bad request from android side
ganesh
Define "not able to recognise"? Is an exception being thrown? If so, what is it? What are the details? I'm trying to push you here to give as much information as to exactly what it is you're seeing here as the pointer to the answer is *probably* in an error message somewhere.
Chris J
the json.tostring() object is not able to recognise the stringbuilder defined in .net rest service.. insert operation could not be accepted from a android client to wcf rest service..
ganesh
Is that the exception? If so, that takes me back to my original answer, and theme in subsequent comments: do you need to declare the parameter as a StringBuilder in C#? Why aren't you declaring this as a simple string?
Chris J
simple string is not being accessible by the android client!! its showing bad request if i give string as a parameter
ganesh