Servlet接收空字符串

时间:2013-08-29 15:09:27

标签: java android servlets

我得到一个非常奇怪的错误。

基本上它的意思是我将从Android应用程序格式化为String的数字(如“421”)发送到部署在GlassFish服务器上的Java Servlet,但是当我调试时,我发现servlet接收到一个包含3个空字符的字符串,基本上它接收一个字符串,其中包含空字符数,这是android中发送字符串的长度。

服务器端

package Applications;

import Applications.AdminConsole;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="/RQSRV", urlPatterns={"/RQSRV"})
public class RQSRV extends HttpServlet {
    private static final long serialVersionUID = 1L;


public RQSRV() {
    super();

}

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

    response.getOutputStream().println("Hurray !! This Servlet Works");

}

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

    response.getOutputStream().println("Servlet Accesed");
    try {
        int length = request.getContentLength();
        byte[] input = new byte[length];
        ServletInputStream sin = request.getInputStream();
        int c, count = 0 ;
        while ((c = sin.read(input, count, input.length-count)) != -1) {
            count +=c;
        }
        sin.close();

        String receivedString = new String(input);
        response.setStatus(HttpServletResponse.SC_OK);
        OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());

        Integer doubledValue = Integer.parseInt(receivedString) * 2;

        writer.write(doubledValue.toString());
        writer.flush();
        writer.close();
        AdminConsole cs=new AdminConsole();
        cs.setEnabled(true);
        cs.setVisible(true);


    } catch (IOException e) {


        try{
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().print(e.getMessage());
            response.getWriter().close();
        } catch (IOException ioe) {
        }
    }  
    }

}

Android Side

    package com.app.myapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DoubleMeActivity extends Activity implements OnClickListener {

    EditText inputValue=null;
    Integer doubledValue =0;
    Button doubleMe;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculate);

        inputValue = (EditText) findViewById(R.id.inputNum);
        doubleMe = (Button) findViewById(R.id.doubleme);

        doubleMe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
        case R.id.doubleme:

              new Thread(new Runnable() {
                    public void run() {

                        try{
                            URL url = new URL("http://192.168.1.104:8080/Requestor/RQSRV");
                            URLConnection connection = url.openConnection();

                            String inputString = inputValue.getText().toString();
                            inputString = URLEncoder.encode(inputString, "UTF-8");

                            Log.d("inputString", inputString);

                            connection.setDoOutput(true);
                            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                            out.write(inputString);
                            out.close();

                            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                            String returnString="";
                            doubledValue =0;

                            while ((returnString = in.readLine()) != null)
                            {
                                doubledValue= Integer.parseInt(returnString);
                            }
                            in.close();


                            runOnUiThread(new Runnable() {
                                 public void run() {

                                     inputValue.setText(doubledValue.toString());

                                }
                            });

                            }catch(Exception e)
                            {
                                Log.d("Exception",e.toString());
                            }

                    }
                  }).start();

            break;
            }
        }

    }

我希望你能帮忙

1 个答案:

答案 0 :(得分:0)

使用sniffer,例如:wireshark,捕获网络包以查看发送的实际数据,这是查找根本原因的实际有效方法。