Hallo!
I'm trying to send some json object as postparameters using the
httpclient. Therefore on the (android) client I use the following
code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new
HttpPost("http://10.0.2.2/TestServlet?
testParameter1=Test);
//doesn't work
HttpParams params = new BasicHttpParams();
params.setParameter("data", data2send);
httpPostRequest.setParams(params);
//doesn't work either
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", data2send));
httpPostRequest.setEntity(new
UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Encoding", "gzip");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse)
httpclient.execute(httpPostRequest);
System.out.println("HTTPResponse received in [" +
(System.currentTimeMillis() - t) + "ms]");
On the server side, I'm using a servlet and doing the following:
public doPost(HttpServletRequest request, HttpServletResponse
response) throws Exception {
String data = (String)request.getParameter("data");
System.out.println("Data=" +data);
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
System.out.println(paramName);
}
}
Unforunately the variable data is null.
And priting out all parameters only gives me the testParameter1, which
I added to the URL... Do you have any idea, what goes wrong?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en