Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem to read bytes in NanoHttpd post with octet-stream (Android) #293

Open
brunogabriel opened this issue Mar 17, 2016 · 2 comments
Open

Comments

@brunogabriel
Copy link

Hello, I am using Nanohttpd in Android to create an app that receives bytes from other instance of the same app. In my case, my application works and send data normally to a web service, but when I tried to send data between two instance of this application, when I debugged I saw strange bytes like trash or for example different encoding. I tried to use InputStream from NanoHttpd.

My code send data with a java code:

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainTest {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://192.168.0.215:8080");
            HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setUseCaches(false);
            httpConnection.setDoInput(true);
            httpConnection.setDoOutput(true);
            httpConnection.setChunkedStreamingMode(4096);
            httpConnection.setConnectTimeout(1000 * 5);
            httpConnection.setReadTimeout(1000 * 5);
            httpConnection.setRequestMethod("POST");
            httpConnection.addRequestProperty("Content-Type", "application/octet-stream");
            httpConnection.addRequestProperty("Transfer-Encoding", "chunked");
            httpConnection.addRequestProperty("Connection", "close");
            OutputStream out = httpConnection.getOutputStream();

            DataOutputStream dataOutputStream = new DataOutputStream(out);
            dataOutputStream.writeInt(37);
            dataOutputStream.close();
            httpConnection.getResponseMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In Android, I want to read bytes from the last code, my code is:

@Override
        public Response serve(IHTTPSession session) {
            Method method = session.getMethod();
            try {
                if (Method.POST.equals(method)) {
                    DataInputStream in = new DataInputStream(session.getInputStream());
                    int value = in.readInt();
                    Log.d("nextValue: ", value); // expected 37
                } else {
                    return executeGet(session);
                }
            } catch (Exception ex) {
                return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT,
                        "SERVER MOBILE INTERNAL ERROR: : " + ex.getMessage());
            }
        }

Can you help me? I need to use main java sample to send bytes, sometimes I need to writeUtf etc., because I've an app that works the same.

Thanks

@brunogabriel
Copy link
Author

Anyone could help? If I don't use ocstream, works correctly, but all my server was written using it.

@LordFokas
Copy link
Member

LordFokas commented May 17, 2016

You need to do a bit more homework, but I'll try to help here.
Follow these steps to debug it:

  • log the whole outgoing and incoming packet in hexadecimal form.
  • isolate the \r\n\r\n sequence (should be 0D0A0D0A or something like that, depending on the encoding)
  • look at the following bytes and understand what you're looking at.
  • check if you're misreading the packet or miswriting it.
  • locate the problem
  • fix it

You should be able to perform at least the first 3 steps. Whether or not you can fix it by yourself, share your findings with us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants