Java Question how to deal "HttpRequestHandler" in library ?

hears

Active Member
Licensed User
Longtime User
public void onCreate() {
mDescriptionHandler = new DescriptionHandler();

addRequestHandler("/onvif*", mDescriptionHandler);
}


when i use this "new DescriptionHandler()" class in B4A, get ERRO ,and app close:

java.lang.NullPointerException: Attempt to invoke virtual method 'void net.majorkernelpanic.http.TinyHttpServer$MHttpRequestHandlerRegistry.register(java.lang.String, org.apache.http.protocol.HttpRequestHandler)' on a null object reference






Java:
package net.majorkernelpanic.http;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URLDecoder;
import java.nio.charset.Charset;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.RequestLine;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;

import anywheresoftware.b4a.BA;
import net.majorkernelpanic.onvif.DeviceBackBean;
import net.majorkernelpanic.spydroid.SpydroidApplication;
import net.majorkernelpanic.streaming.Session;

public class DescriptionHandler implements HttpRequestHandler {
         
     public static final String ONVIF_GET_DEVICE_INFORMATION_REQUEST = "GetDeviceInformation";
       
        public static final String ONVIF_GET_STREAM_URI_REQUEST = "GetStreamUri";
     
        public static final String ONVIF_GET_PROFILES_REQUEST = "GetProfiles";
      
        public static final String ONVIF_GET_CAPABILITIES_REQUEST = "GetCapabilities";

       
        public static final String ONVIF_GET_VIDEO_ENCODER_CONFIGURATION_REQUEST = "GetVideoEncoderConfiguration";

       
        public static final String ONVIF_GET_VIDEO_ENCODER_CONFIGURATION_OPTIONS_REQUEST = "GetVideoEncoderConfigurationOptions";

        private final SessionInfo[] mSessionInfoList = new SessionInfo[10];

        private class SessionInfo {
            public Session session;
            public String uri;
            public String description;
        }

        private SpydroidApplication application = SpydroidApplication.getInstance();

        public DescriptionHandler() {
         
            for (int i = 0; i < 10; ++i) {
                mSessionInfoList[i] = new SessionInfo();
            }
        }

      
        @Override
        public void handle(HttpRequest httpRequest, HttpResponse httpResponse,
                           HttpContext httpContext) throws HttpException, IOException {
            Socket socket = ((TinyHttpServer.MHttpContext) httpContext).getSocket();
            String uri = httpRequest.getRequestLine().getUri();

            BA.Log("handle request of " + uri);
            RequestLine requestLine = httpRequest.getRequestLine();
            String requestMethod = requestLine.getMethod();
            if (requestMethod.equals("POST")) {
                final String requestUrl = URLDecoder.decode(requestLine.getUri());
                BA.Log("the request url are " + requestUrl);
                HttpEntityEnclosingRequest post = (HttpEntityEnclosingRequest) httpRequest;
                byte[] entityContent = EntityUtils.toByteArray(post.getEntity());
                String content = new String(entityContent, Charset.forName("UTF-8"));
                DeviceBackBean deviceBackBean = application.getDeviceBackBean();
                BA.Log("the request back data are " + deviceBackBean.toString());

             
                String backContent = "";
                
                if (content.contains(ONVIF_GET_DEVICE_INFORMATION_REQUEST)) {
                    // 
                    BA.Log("handle the request of get device information");
                    
                    backContent = "";
                } else if (content.contains(ONVIF_GET_STREAM_URI_REQUEST)) {
                    BA.Log("handle the request of get stream url");
                    backContent = "";
                } else if (content.contains(ONVIF_GET_PROFILES_REQUEST)) {
                    BA.Log("handle the request of get device profile");
                    backContent = "";
                } else {
                    // TODO: do not handle this response
                    return;
                }
                BA.Log("the response info are " + backContent);
                final String finalBackContent = backContent;
                ByteArrayEntity backBody = new ByteArrayEntity(finalBackContent.getBytes());
                ByteArrayInputStream backInputStream = (ByteArrayInputStream) backBody.getContent();
                httpResponse.setStatusCode(HttpStatus.SC_OK);
                backBody.setContentType("application/soap+xml; charset=UTF-8");
                httpResponse.setEntity(backBody);
            }
        }
    }
 

hears

Active Member
Licensed User
Longtime User
Truth is that you are wasting your time with this. There is no better solution for making http requests in B4X than with OkHttpUtils2.
this library is a Onvif_HTTP server ,a litte different..
 

DonManfred

Expert
Licensed User
Longtime User
onviv uses soap protocol.
How is it different? You can easily communicate using SOAP with okhttputils2. Search the forum
 

hears

Active Member
Licensed User
Longtime User
thank you
onviv uses soap protocol.
How is it different? You can easily communicate using SOAP with okhttputils2. Search the forum
 
Top