Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

hi guys i'm using the code below to send an http post request which sends an object to a wcf service.This works ok, but what happens if my wcf service needs also other parameters?how can i send them from my android client? this is the code i've written so far:

                  StringBuilder sb = new StringBuilder();  

                String http = "http://android.schoolportal.gr/Service.svc/SaveValues";  


                HttpURLConnection urlConnection=null;  
                try {  
                    URL url = new URL(http);  
                     urlConnection = (HttpURLConnection) url  
                            .openConnection();
                    urlConnection.setDoOutput(true);   
                    urlConnection.setRequestMethod("POST");  
                    urlConnection.setUseCaches(false);  
                    urlConnection.setConnectTimeout(10000);  
                    urlConnection.setReadTimeout(10000);  
                    urlConnection.setRequestProperty("Content-Type","application/json");   

                    urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
                    urlConnection.connect();  

                     //Create JSONObject here
                    JSONObject jsonParam = new JSONObject();
                    jsonParam.put("ID", "25");
                    jsonParam.put("description", "Real");
                    jsonParam.put("enable", "true");
                    OutputStreamWriter out = new   OutputStreamWriter(urlConnection.getOutputStream());
                    out.write(jsonParam.toString());
                    out.close();  

                    int HttpResult =urlConnection.getResponseCode();  
                    if(HttpResult ==HttpURLConnection.HTTP_OK){  
                        BufferedReader br = new BufferedReader(new InputStreamReader(  
                                urlConnection.getInputStream(),"utf-8"));  
                        String line = null;  
                        while ((line = br.readLine()) != null) {  
                            sb.append(line + "\n");  
                        }  
                        br.close();  

                            System.out.println(""+sb.toString());  

                    }else{  
                       System.out.println(urlConnection.getResponseMessage());  
                    }  
                } catch (MalformedURLException e) {  

                    e.printStackTrace();  
                }  
                catch (IOException e) {  

                    e.printStackTrace();  
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{  
                    if(urlConnection!=null)  
                       urlConnection.disconnect();  
                }  
share|improve this question
follow this link. You are trying to send other params, the link blow will give you demonstration how you you can send them after encoding xyzws.com/Javafaq/… – Amir Qayyum Khan Dec 17 '12 at 10:14
up vote 8 down vote accepted

Posting parameters Using POST:-

URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream  input;
url = new URL (getCodeBase().toString() + "env.tcgi");
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");   
urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
urlConnection.connect();  
//Create JSONObject here
JSONObject jsonParam = new JSONObject();
jsonParam.put("ID", "25");
jsonParam.put("description", "Real");
jsonParam.put("enable", "true");

The part which you missed is in the the following... i.e., as follows..

// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.write(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
printout.flush ();
printout.close ();

The rest of the thing you can do it.

share|improve this answer
sorry i accepted your answer but now that i'm trying to implement it,i have encountered problems.Where do you pass your parameters? – libathos Jan 8 at 9:51
jsonParam Object is to send parameters. – Harish Jan 8 at 10:00
ok but i also want to send an object..how can i achieve this? meaning i have to send now 2 json objects – libathos Jan 8 at 10:04
In the same way you can add another object also. – Harish Jan 8 at 10:43
ok sorry for delaying to answer thanks – libathos Jan 8 at 13:55

try some thing like blow:

    SString otherParametersUrServiceNeed =  "Company=acompany&Lng=test&MainPeriod=test&UserID=123&CourseDate=8:10:10";
    String request = "http://android.schoolportal.gr/Service.svc/SaveValues";

    URL url = new URL(request); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
    connection.setUseCaches (false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
    wr.writeBytes(otherParametersUrServiceNeed);

   JSONObject jsonParam = new JSONObject();
                        jsonParam.put("ID", "25");
                        jsonParam.put("description", "Real");
                        jsonParam.put("enable", "true");

    wr.writeBytes(jsonParam.toString());

    wr.flush();
    wr.close();

References :

  1. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
  2. Java - sending HTTP parameters via POST method easily
share|improve this answer
well i did that but i still get a bad request as a server response.Now let me ask if i did everything corect :When i set the otherparameter should i put param1 or the name of my parameters?also,my json object is the first parameter so first i write this one and after that i write the otherparameters is that right? – libathos Dec 17 '12 at 10:59
On server side the param sequence does not matter.It seems you are puting json in payload ... in this otherParametersUrServiceNeed = "param1=a&param2=b&param3=c"; put the name of parameters that you server need this is just example ..You should first identify the parameters that you server need in request the add these params and there values in String otherParametersUrService as my example demonstrates. – Amir Qayyum Khan Dec 17 '12 at 11:09
If it is not confidential you can tell me what addition params your server need ..so that i can put them in above example . – Amir Qayyum Khan Dec 17 '12 at 11:10
Well it's not,it is okay if i give you the method signature??:long InsertStudentAbsences(SRV_Students_Absence objStudentAbsences, string Company, string Lng, string MainPeriod, string UserID, string CourseDate); – libathos Dec 17 '12 at 11:16
do you need something else?? – libathos Dec 17 '12 at 11:43
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.