idish
idish

Reputation: 3260

Http Post not working (C# code working and java code isn't)

Alright, so the http post of the c# code works(the function returns TRUE, means the response string is "OK", here it is:

public bool Rank(int rank)
    {
                    System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
        string postData = "";
        InvokeOnMainThread(delegate(){
        postData="pass=somePass&request=someRequest&access_key="+((FBTabBarController)TabBarController).AAMAccessKey+"&pid="+place_id+"&rank="+rank.ToString();
        });
byte[]  data = encoding.GetBytes(postData);

                HttpWebRequest myRequest =
            (HttpWebRequest)WebRequest.Create("someURL");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
        HttpWebResponse hwr =(HttpWebResponse) myRequest.GetResponse();
        StreamReader reader = new StreamReader(hwr.GetResponseStream());
        string res = reader.ReadToEnd();
        if(res=="OK") 
            return true;}
        else if(res == "FAILED") return false;

        return false;
    }

And here's the JAVA code that isn't working(the function returns FALSE for the same parameters as the code above, the response string is: NULL :

   public boolean SubmitRank(String URL) 
    {
        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(URL); 
            // Add your data   

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); 
            Log.d("MyTag","id: " + place_id + "rank: " + rank);
            nameValuePairs.add(new BasicNameValuePair("pass","somePass"));
            nameValuePairs.add(new BasicNameValuePair("request","someRequest"));
            nameValuePairs.add(new BasicNameValuePair("accesskey",shareAppPreferences.getAccessKey()));
            nameValuePairs.add(new BasicNameValuePair("pid",place_id));
            nameValuePairs.add(new BasicNameValuePair("rank",rank));

            try {
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
                    try {
                       HttpResponse response = httpclient.execute(httppost);
                        String resString = EntityUtils.toString(response.getEntity());

                        if(resString.equals("OK")){
                            return true;
                        }
                        else if(resString.equals("FAILED")){
                            return false;
                        }
                        return false;
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            return false;
    }

Why the JAVA code isn't working while the C# code is working? Am I missing anything in the above request?

Upvotes: 0

Views: 1718

Answers (1)

General Waters
General Waters

Reputation: 1139

What's the HTTP status code being returned? You can this obtained via response.getStatusLine().getStatusCode() method. This will help you pin point the possible issue, like is the request even making it to the server.

Otherwise, the way you're creating and sending your HTTP request in the Java code looks correct and valid.

Upvotes: 1

Related Questions