How to send Parameters (some values) from servlet to android - android

on the server side i am doing this and have to send three strings from this class
to the client side in android application
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {
name1 = getParameter(req, "name");
phone1 = getParameter(req, "phone");
dob1 = getParameter(req, "dob");
String regId = getParameter(req, PARAMETER_REG_ID);
resp.setContentType("text/html");
Datastore.register(regId);
Datastore.register_name(name1);
Datastore.registerPhone(phone1);
Datastore.registerDob(dob1);
}
And On The Client Side I have to Recieve it
Actually i was doing it to get responses from server to client it has relay simple solution what you want to send from server to client side if you have post method in the do post method u have to make some output stream and what ever you write on the stream you can get easily by reading that output stream

yes u can use this way on servlet response
resp.setHeader(java.lang.String name, java.lang.String value)
and at Android end you can get the value from response
String value =response.getHeaders(name);
I hope u will get into the way i told u .....

Your question is not clear ...
Please edit your question and explain in detail
What I have understood is: You want to send some messages / parameters from your back end Server to the Mobile device.
If this is what you need, then have a look at the Google Cloud Messaging : http://developer.android.com/google/gcm/index.html which is designed specifically for this.

Related

Getting Server Error when Posting empty String in Volley

In the post request I am posting some parameter to the server as a request body . But unable to post empty string . So the question is that can we make post request with empty string if yes then how?
My code snippet is ..
HashMap<String, String> paramList = new HashMap<>();
paramList.put("ApplicationCode", Constant.Application_Code);
paramList.put("LoginType", Constant.getLoginType(spnLoginType.getSelectedItem().toString()));
paramList.put("BrandCode", Constant.BRAND);
paramList.put("CountryCode", Constant.CountryCode);
paramList.put("CompanyId", etCompanyId.getText().toString().toUpperCase());
paramList.put("UserId", etUserId.getText().toString().toUpperCase());
paramList.put("AppVersion", Constant.API_VERSION_VALUE);
paramList.put("IpAddress", "");
paramList.put("IpAddress", ""); here I am posting empty String that
does not work
And if I post like this paramList.put("IpAddress", "null"); worked
fine.
In the server-side validation, information is being sent to the server
and validated using one of server-side languages. If the validation
fails, the response is then sent back to the client .
But when send as a String does not worked
Why ?
Sloppy mistake coming from server side. Rectify your Server-side Validation .

Android HTTP Link stop reading when there is a " ",() ,etc

I try to send the user key in String to onine server and make request, but it just send the string before the symbols. Any idea how to make Android read through but not remove the symbols? I mean all the symbols.
The following method is replace the [space] so the PHP can read my link
String sText = ""+autoTv.getText().toString().replace(" ","%20")+"";
SearchRequest t = new SearchRequest();
t.execute(sText);

Sending a post in android with bad encoding

I'm sending post content to a server from android.
The problem is that the data at the server arrives wrong, with encoding problems, for example "{" arrives as "%7B%".
This is the code from android:
RequestParams params = new RequestParams();
params.put("alta", "{s}");
String ruta = "http://www.something.com/receive";
client.post(ruta, params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
}
}
The server part is just receiving this data, like:
$data = $this->request->data;
$data =file_get_contents('php://input');
This issue is not directly related to text encoding per se.
As can be seen from the docs for RequestParams, text values are directly included in the url. As all text that is included in URLs has to be encoded to only include characters that are allowed in URLs (ASCII), text is url encoded.
AsyncHttpClient automatically does that encoding in the background, so you receive the strings in encoded form on the php side.
In order to get the original text you sent, you can use the rawurldecode() or urldecode() function on the php side to decode the encoded string you receive.
You need to use URLEncoder.encode(...) the the data part of you request.
At the server URL decode it.
You should be fine.

Android http post send with different charset encoding

i've got a problem do a http post on android with german "umlaute" äöü. I am passing a json object to the method below and execute the returned ClientResource with post and the request entity in the returned client response. When I want to post something like { "foo":"bär" } the HttpClient sends something like { "foo":"b√§r" }.
Don't know why. What am I doing wrong.
public static ClientResource newPostRequest(Context context, String urn,
JSONObject form) throws MissingAccessTokenException {
ClientResource resource = new ClientResource(uri + urn);
StringRepresentation sr = new StringRepresentation(form.toString());
sr.setMediaType(MediaType.APPLICATION_JSON);
resource.getRequest().setEntity(sr);
return resource;
}
Update
I used the default android http client (which is a apache http client I believe) and got the same error. So the problem might be located here. I try to implement another json parser (currently gson) and (if possible) another http client. Be back later...
Update
Gson is not the problem. I added a json String to the StringRepresentation and nothing changed.
ANSWER
Well that's an odd one. Maybe someone can clear this for me. I always asked myself, why √§ where used and I figured out that translating the utf-8 ä leads to √§. Obviously my android phone did not use macroman, but my mac did. I therefor changed the text file encoding in eclipse, restarted eclipse and the tomcat server and it worked. Still the TCP/IP Monitor in eclipse uses mac roman which looks still wrong. It was thereby a problem with my server, not with the restlet client on android. I just couldn't see it because the TCP/IP Monitor encoded everything in macroman.
did you try calling setCharacterSet(...) on your StringRepresentation? e.g.,
StringRepresentation sr = new StringRepresentation(form.toString());
sr.setCharacterSet(CharacterSet.UTF_8);

Reading Data Sent by Android Client using Servlet

I am sending data from android client to a server
httpPost.setEntity("Some String");
or
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
I want to retrieve this string or namevaluepairs or a hashmap.
I want to use a servlet instead of php scripting. I had looked at all methods of HttpRequest but missing something.
I am running the app in emulator with a tomcat local host and with 10.0.2.2 ip from emulator.
please provide a sample for such servlet. Thanks in advance
In your servlet call request.getParameter("parameterName") to retrieve the parameters sent from your Android device. The parameter name is the same name you use in your name/value pairs.
Assuming you are using a POST (if not, override doGet instead), your servlet should look like this:
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = request.getParameter("parameterName");
... more
}
}

Categories

Resources