alwx / react-native-http-bridge

HTTP server for React Native
118 stars 78 forks source link

Error: java.lang.Double cannot be cast to java.lang.String #7

Closed saberprashant closed 5 years ago

saberprashant commented 5 years ago

Hi there, @alwx @janherich @mandrigin @yenda

I am trying to use this package and I followed that little documentation, but I am getting this error on running the app on android device(Redmi Note 5). I don't know what is the exact problem, but I tried to search for it and did some trial and error, so I figured out that the code inside componentWillMount() is creating the problem.
Currently, my app code is:

import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import httpBridge from 'react-native-http-bridge';

class App extends Component {
  componentWillMount() {
    // initalize the server (now accessible via localhost:1234)
    httpBridge.start(5561, (request) => {

        // you can use request.url, request.type and request.postData here
        if (request.type === "GET" && request.url.split("/")[1] === "users") {
          httpBridge.respond(200, "application/json", "{\"message\": \"OK\"}");
        } else {
          httpBridge.respond(400, "application/json", "{\"message\": \"Bad Request\"}");
        }

    });
  }

  componentWillUnmount() {
    httpBridge.stop();
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.instructions}>To get started</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'lightgrey',
  },
  welcome: {
    fontSize: 50,
    textAlign: 'center',
    margin: 20,
    color: 'red'
  },
  instructions: {
    textAlign: 'right',
    color: '#333333',
    marginBottom: 5,
  },
});

export default App;

Please help me with this, as I really need to use this in my app.
Thanks :)

k90s commented 5 years ago

Add a service name will fix it.

httpBridge.start(5561, "service name", (request) => {...

saberprashant commented 5 years ago

Hey @kai-ding , thanks for your answer.
Where do I need to make this service, and what should I put in this service?
Thanks :)

dsendri commented 5 years ago

@kai-ding thanks for the fix, it works for me

@saberprashant you put the service name as second argument for httpBridge.start(5561, "SERVICENAME", at componentWillMount()

saberprashant commented 5 years ago

Hey @kai-ding @dsendri , I tried the suggested fixes, now I am getting this error again which I got in starting, The development server returned response error code: 500.
I tried to solve this error following these links but no luck :
https://github.com/react-community/create-react-native-app/issues/401 https://github.com/facebook/react-native/issues/21310

Thanks :)

saberprashant commented 5 years ago

Hey @kai-ding @dsendri , thank you very much guys, finally it is working.
Now, I am facing one more issue, like when I send a POST request with BODY from POSTMAN to my device(where the server is running). it shows response as OK(200), but I don't see data in my console logs, and to see the data I opt for Debug JS Remotely option in the native app menu, and when the debugger opens in my chrome, no console logs are visible. Also, after this, if I send a new request from POSTMAN then request fails.
I don't know what is happening now.
Please help :)

saberprashant commented 5 years ago

Hey @kai-ding @dsendri , thanks once again for your help. Finally, it is working good fr me right now as I also solved some last issues which I mentioned in the above comment,
Thanks everyone for your time :)

stevenalanstark commented 5 years ago

Please update the readme instructions, I just ran into this same issue caused by following the outdated readme.

saberprashant commented 5 years ago

Hey @stevenalanstark , somebody has already sent a pull request for the same, I hope they will accept it soon. Well, if you run into any other issue related to http-bridge then do ping me, as I successfully made this module to work for me. Thanks :)