servlet中的JSONObject错误

时间:2015-06-18 18:49:52

标签: java json servlets client server

我已经创建了一个Servlet类,用于通过android客户端接收字符串。此类将此字符串发送到另一个servlet类。 使用REST API我正确地接收了字符串,但是当我创建JSONObject时,它会抛出java.lang.ClassNotFoundException:net.sf.json.JSONObject。 这里实现了代码:

             protected void doPost(HttpServletRequest request,                 HttpServletResponse response) throws ServletException, IOException {

            PrintWriter out= response.getWriter();

                ArrayList<String> arrivo;
                ArrayList<String> preference=new ArrayList<String>();              
                      StringBuffer jb = new StringBuffer();
                      String line = null;
                      try {
                        BufferedReader reader = request.getReader();

                        while ((line = reader.readLine()) != null)
                          jb.append(line);
                        preference.add(line);
                        response(response,"ok");
                        System.out.println(response);
                      }
    catch (Exception e) { //report an error

                   }

                      response(response,"no");

                    try {

                             String s=jb.toString();

                             arrivo=new ArrayList<String>(Arrays.asList(s.split(",")));

                      } catch (ParseException e) {

                       throw
                       new IOException("Error parsing JSON request string");
                      }
                     invio(arrivo);

                            }      
    public void invio(ArrayList<String> c){

            String pref="";
            String x=c.toString();


            StringBuilder sb = new StringBuilder();  

            String http = "http://mioserver:80808";  

            HttpURLConnection urlConnection=null;  
            try {  
                URL url = new URL(http);  
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);  
                urlConnection.setRequestMethod("POST");  
                urlConnection.setUseCaches(false);  

               urlConnection.setConnectTimeout(10000);  
                urlConnection.setReadTimeout(10000);  
                urlConnection.setRequestProperty("Content-Type","application/json");  

                //urlConnection.setRequestProperty("Host","http://localhost:8080/W7TurismoServer/Servlet");

                urlConnection.connect();


               JSONObject object=new JSONObject();

               object.put("",pref);
               OutputStreamWriter out = new     OutputStreamWriter(urlConnection.getOutputStream());
                out.write(object.toString());
                System.out.println(object.toString());
                out.close();  
                int HttpResult =urlConnection.getResponseCode();  
                if(HttpResult ==HttpURLConnection.HTTP_OK){  

                    BufferedReader br = new BufferedReader(new InputStreamReader(  
                        urlConnection.getInputStream(),"utf-8"));  
                    String line = null;  
                    while ((line = br.readLine()) != null) {  
                        sb.append(line + "\n");  
                    }  
                    br.close();  


                    System.out.println(""+sb.toString());  

                }else{  
                        System.out.println(urlConnection.getResponseMessage());  
                }  

                } catch (MalformedURLException e) {  

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

                e.printStackTrace();  
                }
                }

我该如何解决这个问题?先感谢您。

0 个答案:

没有答案