Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
static String deviceID="30000000",controllerDeviceID="null",partnerMode=null,phoneNumber="9578010375",phoneOS="ICS",phoneModel="MotoG",createdOn="124566",status="Active";

Details details;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      HttpAsyncTask h=new HttpAsyncTask();
      h.execute();
}
public static String POST(String url)
{

    InputStream inputStream=null;
    String json = "";
    String result=null;
   try {
       HttpClient httpClient=new DefaultHttpClient();
       HttpPost httpPost=new HttpPost(url);
       JSONObject jsonObject = new JSONObject();
       jsonObject.accumulate("DeviceID",deviceID);
       jsonObject.accumulate("ControllerPhoneNumber",controllerDeviceID);
       jsonObject.accumulate("PhoneNumber",phoneNumber);
       jsonObject.accumulate("DeviceOS",phoneOS);
       jsonObject.accumulate("DeviceModel",phoneModel);
       json=jsonObject.toString();
       StringEntity se=new StringEntity(json);
       httpPost.setEntity(se);
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
       httpPost.setHeader("Content-type", "application/json");
       HttpResponse httpResponse = httpClient.execute(httpPost);
       se.setContentEncoding(HTTP.UTF_8);
       inputStream = httpResponse.getEntity().getContent();
       Log.d("OUTPUT",httpResponse.getStatusLine().toString());
       if (inputStream!=null)
           result=convertInputStreamToString(inputStream);

       else
           result="Did not work";
   }
   catch (Exception e)
   {
       Log.v("InputStream",e.getLocalizedMessage());

   }
    return result;
}

private class HttpAsyncTask extends AsyncTask<string,void,string>

{

    @Override
    protected String doInBackground(String... urls) {
        POST(url);
        return null;
    }
    @Override
    protected void onPostExecute(String result)
    {
        Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
    }
}

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

    inputStream.close();
    return result;

}
Posted
Updated 21-May-15 21:14pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900