2016-10-27 5 views
0

私は自分のコードについて質問があります。それは完全に エラーを仕事doesntの
java.io.StreamCorruptedException

ObjectInputStream obin=new ObjectInputStream(response.getEntity().getContent()); 

これは、Javaサーブレットです私の男

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     loginButton=(Button) findViewById(R.id.button1); 
     loginButton.setOnClickListener(this); 

     username=(EditText) findViewById(R.id.editText1); 
     password=(EditText) findViewById(R.id.editText2); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View arg0) { 
     if (arg0== loginButton) { 
      loadSettings(); 
     } 
    } 

    private void loadSettings(){ 
     try { 
      String uname=username.getText().toString(); 
      String upass=new String(password.getText().toString()); 

      if (uname.equals("") || uname.equals(null) ||upass.equals("") ||upass.equals(null)) { 
       Toast.makeText(getBaseContext(), "Please Enter Username and Password", Toast.LENGTH_SHORT).show(); 
      }else{ 
       postParameters=new ArrayList<NameValuePair>(); 
       postParameters.add(new BasicNameValuePair("uname", uname)); 
       postParameters.add(new BasicNameValuePair("upass", upass)); 

       post("http://192.168.1.3:8080/and/log", postParameters); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(getBaseContext(), e.toString()+".....Load", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private void post(String url, ArrayList<NameValuePair> postParameters){ 
     try { 



      HttpClient client=getHttpClient(); 

      HttpPost request=new HttpPost(url); 

      UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(postParameters); 

      request.setEntity(formEntity); 

      HttpResponse response=client.execute(request); 

      ObjectInputStream obin=new ObjectInputStream(response.getEntity().getContent()); 

      //ObjectInputStream obin=new ObjectInputStream(response.getEntity().getContent()); 

      final HashMap hm=(HashMap) obin.readObject(); 

      if (hm.get("status").toString().equals("OK")) {    
       Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_SHORT).show(); 
      }else if(hm.get("status").toString().equals("NO")) {     
       Toast.makeText(getBaseContext(), "Not Success", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(getBaseContext(), e.toString()+"...............", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private HttpClient getHttpClient(){ 
     if (mHttpClient == null) { 
      mHttpClient= new DefaultHttpClient(); 
      HttpParams params=mHttpClient.getParams(); 
      HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT); 
      HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT); 
      ConnManagerParams.setTimeout(params, HTTP_TIMEOUT); 
     } 
     return mHttpClient; 
    } 

を助けてください、このラインから来

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out = response.getWriter()) { 

     try { 


      String uname=request.getParameter("uname"); 
      String upass=request.getParameter("upass"); 
      System.out.println(upass); 

      ObjectOutputStream obut = new ObjectOutputStream(response.getOutputStream()); 
      HashMap hm = new HashMap(); 

      if(uname.equals("p") && upass.equals("123")){ 
      hm.put("status", "OK"); 
      }else{ 
      hm.put("status", "NO"); 
      } 
      obut.writeObject(hm); 
     } finally { 
      out.close(); 
     }} 

    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 


    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response);  
    } 
} 

答えて

0
httpResponse = defaultHttpClient.execute(httpGet); 
     statusCode = httpResponse.getStatusLine().getStatusCode(); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     String strResponse = EntityUtils.toString(httpEntity); 

まず、文字列の中にあなたを変換しますあなたが望むことをするでしょう。例えば私はjsonオブジェクトに変換しています。

JSONObject jsonObject = new JSONObject(strResponse.trim()); 
関連する問題