Learn Android for Free,issues for developers, mobengineers website provide complete solutions for developers issues posts.It also provides online training for android development

Sunday, February 24, 2013

HttpClient post in Android

To post the data to the server, to get the data from the server which is stored in remote database, we can use HttpClient for webservice requests.
 call HttpClient ,
HttpClient  client = new DefaultHttpClient();
Here is the example for HttpClient for posting the data, getting response from the server.

public class MainActivity extends Activity {
    TextView tv;
    String text;
    Button b;
    DefaultHttpClient client; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv     = (TextView)findViewById(R.id.textView1);
     text     = "";
    }
     public void callWebService(View webService)
    {
      try {
            postData();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           }
  public void postData() throws JSONException{ 
        // Create a new HttpClient and Post Header
        client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.0.1:8080/webservice/latest");        
        JSONObject json= new JSONObject();
           try {
            // JSON data:
                json.put("datapost",subJSON);
         StringEntity entity = new StringEntity(mainJSON.toString(), HTTP.UTF_8);
            httppost.setHeader("Content-Type","application/json");
            httppost.setEntity(entity);
            // Execute HTTP Post Request
            System.out.print(json);
            Log.e("tag", text);
            HttpResponse response = client.execute(httppost,localContext);
                      // for JSON:
            if(response != null)
            {
                InputStream is = response.getEntity().getContent();

                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();
                    }
                }
                text = sb.toString();
            }
            tv.setText(text);
 }catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

0 coment�rios:

Post a Comment

20% off

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

NDTV Gadgets360

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts