Muhammmad Hadi Rajani
Muhammmad Hadi Rajani

Reputation: 611

Use HttpURLConnection instead of HttpPost

I have a code written in java for android app. by using HttpPost and DefaultHttpClient library. Currently, i am recoding it to replace the HttpPost and DefaultHttpClient library with HttpURLConnection, as DefaultHttpClient has been depricated. I have done it for one my project, and it worked.

But in the current project I am not getting same response from the webservice upon using HttpURLConnection instead of DefaultHttpClient. Would any one help me please where I'm doing mistake?

Here is the old code:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String postParameter = "Param1=" + Value1 + "&Param2="+ Value2+ "&Param3="+Value3;
try {
 httpPost.setEntity(new StringEntity(postParameter));
} catch (Exception e) {
 e.printStackTrace();
}
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

And here is my new code

_Url = new URL(Url);
HttpURLConnection urlconnection = (HttpURLConnection) _Url.openConnection();
urlconnection.setRequestMethod(Type);
urlconnection.setConnectTimeout(Timeout);
urlconnection.setUseCaches(false);
urlconnection.setDoInput(true);
urlconnection.setDoOutput(true);
 String postParameter = "Param1=" + Value1 + "&Param2="+ Value2+ "&Param3="+Value3;
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os));
writer.write(postParameter);
writer.flush();
writer.close();
os.close();
urlconnection.connect();

The code is running without any error, but the webservice is not giving same response as it is giving for the old code.

Upvotes: 0

Views: 3720

Answers (2)

Lochana Ragupathy
Lochana Ragupathy

Reputation: 4320

You are not getting the input stream, try the below code

         try {

                String postParameter = "Param1=" + Value1 + "&Param2="+ Value2+ "&Param3="+Value3;
                URL url = new URL(UrlStr);
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Accept",
                        "application/json");
                urlConnection.setRequestProperty("Content-Type",
                        "application/json");// setting your headers its a json in my case set your appropriate header

                urlConnection.setDoOutput(true);
                urlConnection.connect();// setting your connection

                OutputStream os = null;
                os = new BufferedOutputStream(
                urlConnection.getOutputStream());
                os.write(postParameter.toString().getBytes());
                os.flush();// writing your data which you post


                StringBuffer buffer = new StringBuffer();
                InputStream is = urlConnection.getInputStream();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is));
                String line = null;
                while ((line = br.readLine()) != null)
                    buffer.append(line + "\r\n");
                     // reading your response 

                is.close();
                urlConnection.disconnect();// close your connection
                return buffer.toString();

            } catch (Exception e) {
            }

Upvotes: 2

Prachi
Prachi

Reputation: 2559

try this

List nameValuePairs = new ArrayList(3);
            nameValuePairs.add(new BasicNameValuePair("Param1", value1));
            nameValuePairs.add(new BasicNameValuePair("Param2", value2));
            nameValuePairs.add(new BasicNameValuePair("Param3", value3));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);

OutputStream post = request.getOutputStream();
            entity.writeTo(post);
            post.flush();

Upvotes: 0

Related Questions