首先,请不要重复我的问题,因为我四处张望,找不到对我有益的东西。我实际上对Java EE和node.js完全不感兴趣。但是我有一个项目,成功的时间非常有限,因此我需要您的帮助。 到目前为止,我在Android上的成功使用了旧版库org.apache.http.legacy,因为我找到了使用该库完成的解决方案。无论如何,通过使用此库,我可以将图像成功发送到localhost中的服务器。服务器正在使用node.js。在服务器中,我将图像保存在Images文件夹中。我写了python代码,即CNN.py。(我稍后将此图像路径作为参数提供给CNN.py,只是想确保CNN.py正常工作。)Python代码运行良好。不久,我将android中的图像发送到node.js服务器。我收到图像并将图像保存在/ Images文件夹中。
问题是,CNN.py代码产生了一个输出,我可以在node.js中接收到,但是我想将其发送回Android的几乎所有方式都无法正常工作。我不知道为什么实际上,DownloadJSON类不起作用。我只想检索node.js产生的数据
请注意我对node.js和JavaEE不了解。我只需要立即完成。另外,请明确您的答案。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int RESULT_LOAD_IMAGE = 1;
private static final String SERVER_ADDRESS = "http://192.168.43.159:8000/api/upload";
String satir;
ImageView imageToUpload , downloadedImage;
Button bUploadImage, bDownloadImage;
EditText uploadImageName, downloadImageName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
downloadedImage = (ImageView) findViewById(R.id.downloadedImage);
bUploadImage = (Button) findViewById(R.id.bUploadImage);
bDownloadImage = (Button) findViewById(R.id.bDownloadImage);
uploadImageName = (EditText) findViewById(R.id.etUploadName);
downloadImageName = (EditText) findViewById(R.id.etDownlaodName);
imageToUpload.setOnClickListener(this);
bUploadImage.setOnClickListener(this);
bDownloadImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.imageToUpload:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);
break;
case R.id.bUploadImage:
Bitmap image = ((BitmapDrawable) imageToUpload.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
break;
case R.id.bDownloadImage:
downloadImageName.setText("image is downloading...");
new DownloadJSON().execute(SERVER_ADDRESS);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data !=null){
Uri selectedImage = data.getData();
imageToUpload.setImageURI(selectedImage);
}
}
private class UploadImage extends AsyncTask<Void,Void,Void>{
Bitmap image;
String name;
public UploadImage(Bitmap image, String name){
this.image=image;
this.name=name;
}
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("image",encodedImage));
dataToSend.add(new BasicNameValuePair("name",name));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS);
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(),"Image Uploaded",Toast.LENGTH_SHORT).show();
}
}
private HttpParams getHttpRequestParams(){
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, 1000*30);
HttpConnectionParams.setSoTimeout(httpRequestParams, 1000*30);
return httpRequestParams;
}
private class DownloadJSON extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
try{
URLConnection connection = new URL("http://192.168.43.159:8000/").openConnection();
connection.setConnectTimeout(1000 * 30);
connection.setReadTimeout(1000 * 30);
return (String) connection.getContent();
}catch(Exception e){
e.printStackTrace();
}
return "errr";
}
protected void onPostExecute(String s) {
Log.d("post Execute ",s);
downloadImageName.setText(s);
}
}
}
这是node.js代码
enter var Express = require('express');
var bodyParser = require('body-parser');
var fs = require("fs");
var app = Express();
//var val;
app.use(bodyParser.json(bodyParser.json({limit: "50mb"})));
app.use(bodyParser.urlencoded({limit: "50mb", extended:
true,parameterLimit:50000}));
var imageFolderPath = "./Images/";
//app.get("/", function(req, res) {
// res.send(val);
//});
app.post("/api/upload", function(req, res) {
var base64Image = req.body.image;
var imageName = req.body.name;
fs.writeFile(imageFolderPath + imageName + ".jpg",
Buffer.from(base64Image, "base64"), function(file, err) {
if(err) {
console.log("Couldn't write file!");
res.status(500).send({ status: "failed", msg: "Couldn't write file!"
});
}
console.log("File is uploaded");
var spawn = require("child_process").spawn;
var pythonProcss = spawn('python',["./servercnn.py"]);
pythonProcss.stdout.on('data',function(data){
val = data.toString();
console.log(val);
//res.send(data.toString());
});
//res.status(200).send({ status: "success", msg: "file is uploaded"
});
});
});
app.listen(8000, function(a) {
console.log("Listening to port 8000");
});