In android i wrote a program that sends a string value to a servlet post method.
in the servlet, by using request.getParameter() i can receive the data.
Is the same applied to struts2? how to get the request parameter string in struts2 or anyother way to obtain the request parameter string.
You would create a variable in your action and then supply a public setter for it. If you need to expose the variable to a view (JSP page), then you will also want to provide a getter.
For example:
private String myValue;
public void setMyValue(final String myValue) {
this.myValue = myValue;
}
Then just pass the variable to the URL for the action. e.g., http://yoursite.com/youraction?myValue=easy
Struts will automatically invoke the setter and pass the value of the form parameter.
Related
I am using retrofit in my android application but my service sometimes return object data type and sometime array datatype. how can i handle this ? i used object in place of data type in android but am not able to use it properly.
Create an Interface,inside the Interface whenever your service returns a List do like this:
public Interface EndPointInterface{
#GET(Constants.URL_GET_PHARMACY_REPORT)
Call<List<PharmacyReport>> getPharmacyReport(#Query(Constants.PATIENT_ID) String patientId);
}
else if your service returns an Object proceed like this:
public Interface EndPointInterface{
#GET(Constants.URL_FETCH_STORE_INFO)
Call<Store> getStoreInfoByBeaconUUID(#Query(Constants.BEACON_UUID) String beaconUUID);
}
I have a Json in a server response which has the entire intent data. I also get extras to be added to the intent.
Now, the extras can vary in data type. int, long, boolean etc.
How do I pass these extra values to the intent which I create in the necessary type?
The putExtra method has string as a key and a few overloads based upon the type of value.
How do I decide which one to use?
Parse the json using gson library http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
create the model class and implements serializable.
Example:-
public class TemplateCommonMetaData implements Serializable {
#SerializedName("code")
public boolean sCode;
#SerializedName("message")
public String message; }
and set serializable objet in intent
in.putExtra("key", serializableobject);
It depends on contracts of json parser, Intent receiver.
If protocol is undefined - heuristically (switch, if/else).
Also you can try gson, it doesn't solve problem with types, but transit can be easier.
In this below code, if I access getBl_no () its correctly returning value. I don't know how it's working I didn't set bl_no anywhere is that Json property will set to that set method? Please anyone explain.
#JsonProperty("BL_NO")
private String bl_no;
public String getBl_no() {
return bl_no;
}
public void setBl_no(String bl_no) {
this.bl_no = bl_no;
}
#JsonProperty annotation is where the magic happens! The JSON parser that you use reads the JSON property named BL_NO and assigns its value to the private instance variable bl_no. You don't even need a setBl_no() method for this to work.
#JsonProperty annotation lets you tell the JSON parser that while serializing or deserializing, the JSON property BL_NO should be tied to variable bl_no. That's how the variable gets initialized with a value even though you don't explicitly do it.
I am using Retrofit to Post form data and recieve back XML. What I have so far works fine but i want to make some changes. Here is my existing code (and it works):
Here is my interface
public interface SignupUser
{
#FormUrlEncoded
#POST("/createaccount.cfm")
SignupServerResponse signup(#Field("e") String email, #Field("p") String password);
}
Here is the code to call the api (again, this works fine, I will explain below what I want to change)
SignUpDetails mDeets; // this gets initialize and set somewhere else
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://myurl.com")
.setConverter(new SimpleXMLConverter()).build(); // the response is xml, that works fine
SignupUser service = restAdapter.create(SignupUser.class);
SignupServerResponse res = service.signup(mDeets.getE(), mDeets.getP());
How can I make it so that I can pass the SignUpDetails object straight to the signup() method instead of passing in separate Strings? When I change the constructor of signup() to accept SignUpdetails object (see below) and pass my SignUpDetails object in, I get an error saying
No Retrofit Annotation Found
Here is how I would like to define the interface
public interface SignupUser
{
#FormUrlEncoded
#POST("/createaccount.cfm")
SignupServerResponse signup(SignUpDetails deets);
}
And call it like this (instead of passing in all those parameters)
SignupServerResponse res = service.signup(mDeets);
I tried adding #Field above each of my variables in the SignUpDetails class and that doesnt work either (compilation error)
On your interface use #Body annotation for the deets parameter:
#POST("/createaccount.cfm")
SignupServerResponse signup(#Body SignUpDetails deets);
That should convert SignUpDetails into XML (since you use xml converter on your adapter).
It is then your responsibility to parse the XML request body on server.
In android, I'm using model classes with methods to handle the data manipulation. My data is brought in from webservices as json. I'm contemplating the possibility of using JSONObjects to store the values of class level attributes. But, I don't know of a way to use the JSONObj as the "holder" variable and create access methods. I don't want to predetermine these methods, as jsonRepository should hold the values, not always known at design time
For example, I'd like to have:
public class User {
private JSONObject jsonAttributes;
public User(String json) {
this.jsonAttributes= new JSONObject(json);
}
[IMPLICIT attribute access methods]
public string Prop1() returns jsonAttributes.getString("prop1");
public string Prop1(String newProp1) returns jsonAttributes.putString("prop1",newProp1);
public string Prop2() returns jsonRepository.getString("id");
public string Prop2(String newProp2) returns jsonAttributes.putString("prop2",newProp2);
....
from outside this class then, I would access the attributes simply...
User myUser = new User(someValidJson);
String myString = myUser.Prop1
Misguided? If not, how does one manage implicit property setting/getting?
As was mentioned in the comment above, why not create your user class, with all of the relevant memeber variables, and simply parse your JSON data in order to populate the ionformation in your user class.
There are a lot of ways you can do this, but I would consider using the builder pattern, as it is flexible, which could be useful if your JSON data changes in the future.