3

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

基本上它是关于我从 Android 应用程序(如“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) {
        }
    }  
    }

}

安卓端

    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;
            }
        }

    }

我希望你能帮忙

4

1 回答 1

0

使用嗅探器,例如:wireshark,抓取网络包,查看发送的真实数据,是一种真正有效的查找根本原因的方法。

于 2013-09-06T16:03:12.687 回答