4

I started developing in Xamarin, and then decided that license may be a bit expensive for playing around, so I'm transferring my code to java.

I have a small chunk that performs a POST with a JSON object, and it works in Xamarin and doest work in Java.

Xamarin:

    var client = new HttpClient ();
    var content = new FormUrlEncodedContent(new Dictionary<string, string>() { 
        {"action", "getEpisodeJSON"},
        {"episode", "11813"}

    });
    client.DefaultRequestHeaders.Referrer = new Uri(link);

    var resp = client.PostAsync("http://www.ts.kg/ajax", content).Result;
    var repsStr = resp.Content.ReadAsStringAsync().Result;
    dynamic res = JsonConvert.DeserializeObject (repsStr);

Android:

    HttpClient httpclient = new DefaultHttpClient();

    // 2. make POST request to the given URL
    HttpPost httpPost = new HttpPost("http://www.ts.kg/ajax");

    String json = "";

    // 3. build jsonObject
    JSONObject jsonObject = new JSONObject();
    jsonObject.accumulate("action", "getEpisodeJSON");
    jsonObject.accumulate("episode", "11813");

    // 4. convert JSONObject to JSON to String
    json = jsonObject.toString();

    // 5. set json to StringEntity
    StringEntity se = new StringEntity(json);

    // 6. set httpPost Entity
    httpPost.setEntity(se);

    // 7. Set some headers to inform server about the type of the content
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    httpPost.addHeader("Referer", "http://www.ts.kg");

    // 8. Execute POST request to the given URL
    HttpResponse httpResponse = httpclient.execute(httpPost);

    // 9. receive response as inputStream
    InputStream inputStream = httpResponse.getEntity().getContent();

    // 10. convert inputstream to string
    String result;
    if(inputStream != null)
        result = convertInputStreamToString(inputStream);

What is a correct way to make such a POST in Android?

UPD Current problem is that i'm getting an empty result string;

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }
Anarion
  • 2,406
  • 3
  • 28
  • 42
  • what is issue? can you post your stack trace to identify the issue? – atish shimpi Jan 16 '15 at 17:13
  • Result string is empty. But when I launch Xamarin app - result string is not empty, as it's supposed to be. – Anarion Jan 16 '15 at 17:16
  • What I also noticed is that code executes really fast, as If no web requests are done at all. (I have a high ping, and I can feel the delay of web requests) – Anarion Jan 16 '15 at 17:17
  • are you sure there is no exception being thrown? – tyczj Jan 16 '15 at 17:18
  • @tyczj, I am hitting a breakpoint right after result = convertInputStreamToString(inputStream); – Anarion Jan 16 '15 at 17:19
  • Also, httpResponse statusCode is 200-OK – Anarion Jan 16 '15 at 17:26
  • This is all happening inside of an Async task correct? – A_Kiniyalocts Jan 16 '15 at 18:21
  • @Anarion Make **result** a local variable, then check the string value in onPostExecute(), is it still null? – A_Kiniyalocts Jan 16 '15 at 20:38
  • @A_Kiniyalocts, I checked it - it's not null, it's empty string. And contentLength of htmlResponse is 0 – Anarion Jan 17 '15 at 08:20
  • @Anarion Can you please paste the contents of `convertInputStreamToString`? Your problem might be there and not in in your http client code. – kha Jan 19 '15 at 08:59
  • @kha, Yes, sure. done it. – Anarion Jan 19 '15 at 09:18
  • @Anarion Thanks. I can't see anything wrong with it to be honest. Do you need the extra headers? Does your service need UserAgent specified in the header? Other than the headers being wrong, I can't see anything wrong with your code. – kha Jan 19 '15 at 10:11
  • @kha, i'm not sure. Is there a way to see the actual request sent from xamarin and from android to compare the difference? – Anarion Jan 19 '15 at 10:37
  • @Anarion Not sure. Never worked with Xamarin before. If the web service is yours (i.e. you can debug into it), your best bet is to intercept it there and have a look at the differences between the headers and the bodies. – kha Jan 19 '15 at 10:48
  • @kha, no, it's a public web service. – Anarion Jan 19 '15 at 11:03

1 Answers1

2

I ended up catching all requests of my device via Fiddle (good tutorial is here: http://tech.vg.no/2014/06/04/how-to-monitor-http-traffic-from-your-android-phone-through-fiddler/)

The difference was in cookie, so I used and HttpContex variable as described here: Android HttpClient Cookie

And I also had a different Content-Type, so I set this header manually as this:

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
Community
  • 1
  • 1
Anarion
  • 2,406
  • 3
  • 28
  • 42