-1

I'm currently trying to send a JSON string from an android app to a C# webservice I made. The webservice code is simply: public void PostLocationsAndroid(string data) { //DO STUFF }

However all my requests end up with data being null. Here's how I'm trying to consume the webservice in the android app:

public void postData(String myValue) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://myurl.com/myserver/PostLocationsAndroid"); try { StringEntity se = new StringEntity(myValue); se.setContentType("application/json; charset=UTF-8"); httppost.setEntity(se); httppost.setHeader("Accept", "application/json"); HttpParams params = new BasicHttpParams(); params.setParameter("data", valueIWantToSend); httppost.setParams(params);
HttpResponse response = httpclient.execute(httppost); }

I've tried several different ways, this is my most recent failure.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Smoore
  • 732
  • 2
  • 10
  • 27
  • Try to call api in advance REST api client. See if you are getting proper response or not. If not then most probably it's issue of web services. – keen May 09 '14 at 13:14

2 Answers2

0

Have you tried the answer from this question? https://stackoverflow.com/a/2938787/389141

It seems to be very similar to the issue you are running into right now.

Community
  • 1
  • 1
Jacob Malliet
  • 525
  • 3
  • 11
  • Yep, I tried a very similar method to that, but I used a StringEntity since I just wanted to post a JSON string. I still get data as being null. I wonder if I'm passing in my parameter incorrectly? – Smoore May 08 '14 at 19:40
  • I'm not sure on that. I haven't ever tried using StringEntity. I had better luck using the answer from that question. If that doesn't work sadly I'm not sure what the issue is :( – Jacob Malliet May 08 '14 at 19:45
  • What did your webservice method look like? – Smoore May 08 '14 at 19:48
0

Are you sure you are passing a JSON string?

Try the below code (I have used similar in my code and works).

 JSONObject jsonObject = new JSONObject();
 jsonObject.put("data", valueIWantToSend);

 HttpResponse httpResponse = handleJsonPost(url, jsonObject);

public HttpResponse handleJsonPost(String url, JSONObject jsonObject)
        throws UnsupportedEncodingException, IOException, ClientProtocolException {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Content-Type", "application/json"); 
    httpPost.setHeader("Accept", "application/json");

    if (jsonObject != null) {
        StringEntity se = new StringEntity(jsonObject.toString());
        httpPost.setEntity(se);
    }

    return new DefaultHttpClient().execute(httpPost);
}
Raghu
  • 351
  • 1
  • 4