Thursday, June 20, 2013

send json object using httpclient

   
                 public void getData()
     {
         dialog = new ProgressDialog(RecentContestActivity.this);
            dialog.setIcon(R.drawable.appicon);
            dialog.setTitle("ForTalent");       
            dialog.setMessage("Loading ...");
            dialog.setIndeterminate(false); 
            dialog.setCancelable(false);
            dialog.show();                                      

         new Thread()
            {
              public void run()
              {
                 try
                       {
                      JSONObject json = new JSONObject();                                                       
                           json.put("call", "contest");
                           json.put("status", "recent");
                           response =  WS.readstream(json);
                  }
                catch (Exception e)
                {
                   
                }
                
                 handler.sendEmptyMessage(0);
               
             }
            }.start();
           
     }
   
     private Handler handler = new Handler() {

         @Override
         public void handleMessage(Message msg)
         {
           
             itmlist = new ArrayList<Object>();
             JSONObject json;
             JSONArray jArray = null;
           
             try
             {               
                 json = new JSONObject(response);
                 jArray = json.getJSONArray("Recent");
                                             
                 if(jArray.length() <= 0)
                 {                                     
                     txterrmsg.setVisibility(View.VISIBLE);
                     txterrmsg.setText("No Recent Contest are available");
                 }
                 else
                 {
                     txterrmsg.setVisibility(View.GONE);
                 }
             }
             catch(Exception e)
             {
               
             }
           
             try
                 {
                 json = new JSONObject(response);
                 jArray = json.getJSONArray("Recent");
                         
                 Recentcontests=new RecentContest[jArray.length()];
            
                 for (int i = 0; i < jArray.length(); i++)
                 {
                     Recentcontests[i]=new RecentContest();
                
                     JSONObject contestobj = jArray.getJSONObject(i);
                
                     Recentcontests[i].contest_Id=contestobj.getString("Contest_id").toString();
                     Recentcontests[i].contest_title=contestobj.getString("Contest_title").toString();
                     Recentcontests[i].contest_description=contestobj.getString("Contest_Desc").toString();
                     Recentcontests[i].contest_bannerurl=contestobj.getString("Contest_bannerURL").toString();
                     Recentcontests[i].contest_starts=contestobj.getString("Contest_starts").toString();
                     Recentcontests[i].contest_ends=contestobj.getString("Contest_ends").toString();
                     Recentcontests[i].contest_status=contestobj.getString("Contest_Status").toString();
                     Recentcontests[i].contest_type=contestobj.getString("Contest_Type").toString();
                    
                 }
            
             }
             catch(Exception e)
             {
                
             }
             //Recentcontests=WS.converttoobjects(response);
           
             for(int i=0;i<Recentcontests.length;i++)
             {
                   itmlist.add(Recentcontests[i]);                     
             }
                 
                  cntstadp = new RecentContestAdapter(RecentContestActivity.this,itmlist);
                  contestlistView.setAdapter(cntstadp);
                  contestlistView.setOnItemClickListener(RecentContestActivity.this);
                  dialog.dismiss();                
         }
     };
public class WSReceiver
{
    public static String mainurl = "http://www.example.com";
    public String readstream(JSONObject json) throws JSONException
    {
        String result=null;
        try
        {
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
            HttpConnectionParams.setSoTimeout(httpParams, 10000);
            HttpClient client = new DefaultHttpClient(httpParams);
          
            String url = mainurl ;

            HttpPost request = new HttpPost(url);
            request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
            request.setHeader("json", json.toString());           
            HttpResponse response = client.execute(request);
           
            HttpEntity entity = response.getEntity();
           
            // If the response does not enclose an entity, there is no need
            if (entity != null)
            {
                InputStream instream = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try
                {
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    try
                    {
                        instream.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }           
                result = sb.toString();           
            }
        }
        catch (Throwable t)
        {
            result = t.toString();
        }
        return result;
    }
   
   

No comments:

Post a Comment