I have a web service which accepts 2 parameters to save json: fileName and a json string. I need to post a json string to this web service. I have tried the method outlined in How to send a JSON object over Request with Android? but it doesn’t seem to work. Any pointers??
public void postDataToServer(String url, String jsonStr) throws ClientProtocolException, IOException
{
int TIMEOUT_MILLISEC = 10000; // = 10 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
httpParams.setParameter("fileName","testFile");
httpParams.setParameter("json",jsonStr);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(
jsonStr.getBytes("UTF8")));
HttpResponse response = client.execute(request);
}
The fileName and json don’t go in the httpParams. They go in the Entity. You should use an HttpEntity, most likely a http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html
with 2 BasicNameValuePair, one for the fileName, one for the json.