Yes. Now we need to use Android to send an Httppost request to Node. js I tried to use PHP to receive Http requests in the past. It looks like this: {code ...} android is like this {code ...} however, in Node. what should I do with js? Does Node. js have a method similar to $ _ POST? Yes.
Now we need to use Android to send an Http post request to Node. js
In the past, I tried to use PHP to receive Http requests, which is like this:
$data = $_POST['data'];
Android is like this.
HttpPost httpRequest = new HttpPost("192.168.1.174:3001"); List
params = new ArrayList
(); params.add(new BasicNameValuePair("data",etv.getText().toString())); try{ httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient() .execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200){ String strResult = EntityUtils.toString( httpResponse.getEntity() ); Log.d("HTTP", strResult); } } catch (Exception e){ e.printStackTrace(); }
But what should I do in Node. js?
Does Node. js have a method similar to $ _ POST?
Reply content:
Yes.
Now we need to use Android to send an Http post request to Node. js
In the past, I tried to use PHP to receive Http requests, which is like this:
$data = $_POST['data'];
Android is like this.
HttpPost httpRequest = new HttpPost("192.168.1.174:3001"); List
params = new ArrayList
(); params.add(new BasicNameValuePair("data",etv.getText().toString())); try{ httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient() .execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200){ String strResult = EntityUtils.toString( httpResponse.getEntity() ); Log.d("HTTP", strResult); } } catch (Exception e){ e.printStackTrace(); }
But what should I do in Node. js?
Does Node. js have a method similar to $ _ POST?
We recommend that you use Express upstairs, so you can get a few lines of code.
Var http = require ("http"); var express = require ("express"); var app = express (); app. post ('/route_you_want', function (req, res) {// You can process post request res here. send ("hello world"); res. end () ;}); http. createServer (app ). listen (3000, function () {console. log ("server start ");});
Some of the post processing methods provided by the system are complicated. For details, Google recommends the web framework, such as express.
Using express alone is not very convenient. You can also try to addMulterMiddleware. I just tried it today. It's quite useful.