-4

url is : http://localhost method type : post header : Content-Type:application/json, decode:2 data : xx

How can we achieve this in android??,and how do i get response from this ?? I saw that the Httpclient is deprecated

Any help would be appreciated

Venky
  • 1,929
  • 3
  • 21
  • 36

4 Answers4

3

Try using HttpUrlConnection

String Url, query;

    InputStream inputStream;
    HttpURLConnection urlConnection;
    byte[] outputBytes;
    String ResponseData;
    Context context;
try{
            URL url = new URL(Url);
            urlConnection = (HttpURLConnection) url.openConnection();
            outputBytes = query.getBytes("UTF-8");
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setConnectTimeout(15000);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.connect();

            OutputStream os = urlConnection.getOutputStream();
            os.write(outputBytes);
            os.flush();
            os.close();

            inputStream = new BufferedInputStream(urlConnection.getInputStream());
            ResponseData = convertStreamToString(inputStream);

    } catch (MalformedURLException e) {

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

            }


    public String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append((line + "\n"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
AbhayBohra
  • 2,047
  • 24
  • 36
1
 URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();

..................................................................

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params)
    {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}
Harsh Singhal
  • 567
  • 4
  • 12
  • thanks a lot,this helps,but how i add content type to this ??,i have a json object – Venky Apr 21 '17 at 09:49
  • getQuery will return string that contain your JSON after that u can extract it. If code helps you then you can mark as a answer thanks – Harsh Singhal Apr 21 '17 at 09:58
0

You can use HttpURLConnection if you dont want to use library otherwise you can use Volley also to make network call because volley make it easier to manage network call! Thanks!

Nikhil Sharma
  • 593
  • 7
  • 23
0

You can use Retrofit Library, as Retrofit is faster and easier than other to load response from HTTP request. visit, https://square.github.io/retrofit/

For Example, Here sample Json Object given,

{
  "title": "foo",
  "body": "bar",
  "userId": 1,
  "id": 101
}  

Code:

public interface APIService {

    @POST("/posts")
    @FormUrlEncoded
    Call<Post> savePost(@Field("title") String title,
                    @Field("body") String body,
                    @Field("userId") long userId);
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Krishna Vyas
  • 1,009
  • 8
  • 25