Advertisement
Guest User

Post the Idea

a guest
Oct 9th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. public static String postData(String title, String story_bhnd_idea,
  2.             String idea_dtls, int spinnerBudgetId, int spinnerFounderIndexId,
  3.             int spinnercategoryIndexId) {
  4.  
  5.         final String url = "URL will be here";
  6.          
  7.         JSONObject jsonPostIdea  = new JSONObject();
  8.        
  9.                          
  10.             JSONObject JSONStoryBhndIdea = new JSONObject();
  11.             JSONArray jsonArrayStoryBhndIdea = new JSONArray();
  12.             try {
  13.            
  14.                 JSONStoryBhndIdea.put("value", story_bhnd_idea);
  15.                 jsonArrayStoryBhndIdea.put(JSONStoryBhndIdea);
  16.                
  17.                 System.out.println("=============> value of the Story Behind Idea:"+jsonArrayStoryBhndIdea.toString());
  18.  
  19.             }
  20.             catch (Exception e) {
  21.             }
  22.            
  23.            
  24.            
  25.             JSONObject JSONIdeaDetails = new JSONObject();
  26.             JSONArray jsonArrayIdeaDetails = new JSONArray();
  27.             try {
  28.            
  29.                 JSONIdeaDetails.put("value", idea_dtls);
  30.                 jsonArrayIdeaDetails.put(JSONIdeaDetails);
  31.                
  32.                 System.out.println("=============> value of the Idea details:"+jsonArrayIdeaDetails.toString());
  33.  
  34.             }
  35.             catch (Exception e) {
  36.             }
  37.            
  38.            
  39.            
  40.             JSONObject JSONBudget = new JSONObject();
  41.             JSONArray jsonArrayBudget = new JSONArray();
  42.             try {
  43.            
  44.                 JSONBudget.put("value", ""+spinnerBudgetId);
  45.                 jsonArrayBudget.put(JSONBudget);
  46.                
  47.                 System.out.println("=============> value of the Budget:"+jsonArrayBudget.toString());
  48.  
  49.             }
  50.             catch (Exception e) {
  51.             }
  52.            
  53.            
  54.            
  55.             JSONObject JSONMyFounder = new JSONObject();
  56.             JSONArray jsonArrayMyFounder = new JSONArray();
  57.             try {
  58.            
  59.                 JSONMyFounder.put("nid", ""+spinnerFounderIndexId);
  60.                 jsonArrayMyFounder.put(JSONMyFounder);
  61.                
  62.                 System.out.println("=============> value of the My Founder:"+jsonArrayMyFounder);
  63.  
  64.             }
  65.             catch (Exception e) {
  66.             }
  67.        
  68.            
  69.            
  70.             JSONObject JSONCategory = new JSONObject();
  71.                    
  72.             try {
  73.  
  74.                 JSONObject Json = new JSONObject();
  75.                 Json.put("tid", ""+spinnercategoryIndexId);
  76.                
  77.                 JSONCategory.put(""+spinnercategoryIndexId, Json);
  78.                
  79.                 System.out.println("=============> value of the category :"+JSONCategory);
  80.                            
  81.             }
  82.                 catch (Exception e) {
  83.             }
  84.              
  85.             /**
  86.              *   JSONObject For the post idea
  87.              */
  88.            
  89.             try {
  90.                 jsonPostIdea.put("type","idea");
  91.                 jsonPostIdea.put("title", title);
  92.                 jsonPostIdea.put("field_story_idea", jsonArrayStoryBhndIdea);
  93.                 jsonPostIdea.put("field_founder_profile_refrence", jsonArrayMyFounder);
  94.                 jsonPostIdea.put("field_idea_budget", jsonArrayBudget);
  95.                 jsonPostIdea.put("field_idea_details", jsonArrayIdeaDetails);
  96.                 jsonPostIdea.put("taxonomy", JSONCategory);
  97.                
  98.                
  99.                 System.out.println("========VALUE FO THE WHOLE POST IDEA PAGE : === >>>>>>>>  ========>"+jsonPostIdea.toString());
  100.                
  101.             } catch (JSONException e) {
  102.                 // TODO Auto-generated catch block
  103.                 e.printStackTrace();
  104.             }
  105.            
  106.        
  107.                            
  108.                     HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
  109.                    
  110.                      HttpResponse response;
  111.  
  112.                     try{
  113.                        
  114.                         HttpPost post = new HttpPost(url);
  115.                          
  116.                         System.out.println("========VALUE FO THE WHOLE POST IDEA PAGE : === >>>>>>>>  === jsonPostIdea.toString() =====>"+jsonPostIdea.toString());
  117.                         StringEntity se = new StringEntity( jsonPostIdea.toString());
  118.                         se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  119.                        
  120.                         post.setEntity(se);
  121.                          
  122.                         response = client.execute(post);
  123.                        
  124.                        
  125.                         int statusCode = response.getStatusLine().getStatusCode();
  126.                         System.out.println("=========> statusCode POST IDEA : ===== => " + statusCode);
  127.                        
  128.                         if (statusCode == HttpStatus.SC_OK) {
  129.                             HttpEntity entity = response.getEntity();
  130.                             InputStream is = entity.getContent();
  131.                            
  132.                             String streamValue = iStream_to_String(is);
  133.                            
  134.                             if (entity != null) {
  135.                                
  136.                                 //post.abort();
  137.                                
  138.                                 //response.getEntity().consumeContent();
  139.                             //  EntityUtils.consume(entity);
  140.                                
  141.                                 /*if(entity.getContentLength() > Constants.HTTP.MAX_APP_CONTENT_LENGTH) {
  142.                                        throw new IllegalArgumentException("HTTP entity too large.");                            
  143.                                     }
  144.                                 String result = EntityUtils.toString(entity,"UTF-8");*/
  145.                                 EntityUtils.consume(entity);                
  146.                                 is.close();
  147.                                
  148.                             }
  149.                            
  150.                             return streamValue;
  151.                            
  152.                         } else {
  153.                             return "Hello This is status ==> :"
  154.                                     + String.valueOf(statusCode);
  155.                         }
  156.                            
  157.                     }
  158.                     catch(Exception e){
  159.                         e.printStackTrace();
  160.                      
  161.                     }
  162.                    
  163.             return url;      
  164.  
  165.     }
  166.      
  167.     public static String iStream_to_String(InputStream is1) {
  168.         BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
  169.        
  170.          
  171.         String line;
  172.         StringBuilder sb = new StringBuilder();
  173.         try {
  174.             while ((line = rd.readLine()) != null) {
  175.                 sb.append(line);
  176.             }
  177.             rd.close();
  178.  
  179.         } catch (IOException e) {
  180.             // TODO Auto-generated catch block
  181.             e.printStackTrace();
  182.         }
  183.         String contentOfMyInputStream = sb.toString();
  184.         return contentOfMyInputStream;
  185.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement