gpt4 book ai didi

javascript - Android 为 Node.js 服务器抛出 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 15:42:12 27 4
gpt4 key购买 nike

我正在尝试将我的 Android 应用程序连接到 Node.js 服务器。对于 URL,我使用了 ifconfig 并发现是 192.168.1.33,因此我在端口 3030 上启动了服务器。

当我从浏览器打开192.168.1.33:3030时,成功获取JSON结果但是当我在 app 中执行相同操作时,它会抛出:

09-09 13:14:38.176  28693-28725/com.example.pournima.loginform W/System.err﹕ java.io.FileNotFoundException: http://192.168.1.33:3030
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at com.example.pournima.loginform.RetreiveStuff.doInBackground(RetreiveStuff.java:43)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at com.example.pournima.loginform.RetreiveStuff.doInBackground(RetreiveStuff.java:18)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-09 13:14:38.184 28693-28725/com.example.pournima.loginform W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-09 13:14:38.185 28693-28725/com.example.pournima.loginform W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
09-09 13:14:38.185 28693-28693/com.example.pournima.loginform D/RESULT :﹕ [ 09-09 13:14:38.218 1089: 1235 V/AlarmManager ]
send {1a636348, *alarm*:com.domobile.applock/.service.LockService}

node.js 命令提示符显示:

Error: invalid json
at parse (/home/mayur/intern/node_modules/body-parser/lib/types/json.js:79:15)
at /home/mayur/intern/node_modules/body-parser/lib/read.js:102:18
at done (/home/mayur/intern/node_modules/body-parser/node_modules/raw-body/index.js:248:14)
at IncomingMessage.onEnd (/home/mayur/intern/node_modules/body-parser/node_modules/raw-body/index.js:294:7)
at IncomingMessage.g (events.js:199:16)
at IncomingMessage.emit (events.js:104:17)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)

我的onCreate():

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.login);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv = (TextView) findViewById(R.id.UserName);
String username = tv.getText().toString();
RetreiveStuff rs = new RetreiveStuff();
try {
String a = rs.execute(username).get();
Log.d("RESULT : ", a);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
});
}

RetreiveStuff.java:

package com.example.pournima.loginform;


public class RetreiveStuff extends AsyncTask<String,Void,String>{

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

}

@Override
protected String doInBackground(String ... params) {

StringBuilder sb = new StringBuilder();
try {
URL url = new URL("http://192.168.1.33:3030");
String username = params[0];
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
OutputStream os = con.getOutputStream();
String toPass = "{\"username\" : \"" + username + "\" }";
Log.d("String passed : " , toPass);
os.write(username.getBytes());
os.flush();

BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
while((line = reader.readLine())!=null){
sb.append(line + '\n');
}
reader.close();

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return sb.toString();

}
}

服务器.js:

var app = require('express')(); // Express App include
var http = require('http').Server(app); // http server
var mysql = require('mysql'); // Mysql include
var bodyParser = require("body-parser"); // Body parser for fetch posted data
var connection = mysql.createConnection({ // Mysql Connection
host : 'localhost',
user : 'root',
password : 'mayur',
database : 'mayur',
});

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.post('/',function(req,res){

connection.query("SELECT * from login where username= ? ", req.body.username ,function(err, rows, fields){
if(rows.length!=0){
res.json(rows);

}else{
var data = 'No users Found..';
res.json(data);
}
});
});

app.get('/',function(req,res){

res.json("You have called get");
});
app.listen(3030);

最佳答案

您仅将 JSON 输出到日志中。您还需要将其传递给请求。

您的代码:

OutputStream os = con.getOutputStream();
String toPass = "{\"username\" : \"" + username + "\" }";
Log.d("String passed : " , toPass);
os.write(username.getBytes());
os.flush();

修复:

OutputStream os = con.getOutputStream();
String toPass = "{\"username\" : \"" + username + "\" }";
Log.d("String passed : " , toPass);
os.write(toPass.getBytes());
os.flush();

修复线路:

os.write(toPass.getBytes());

关于javascript - Android 为 Node.js 服务器抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32474529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com