Socket Python-Server 和 Java - android studio 中的客户端

我已经成功构建了一个 python 服务器,它甚至可以工作,但是当 android studio 的 java 尝试连接到它时,它失败并出现一大堆错误。我知道它在创建新的套接字对象时失败,但我不知道为什么。

这是我的 python 服务器代码:




import socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print("Socket successfully created")


try:

    port = 24224

    s.bind(("", port))

    print("socket binded to %s" %(port))

except socket.error as err:

    print('Bind failed. Error Code : ' .format(err))

    

s.listen(10)


while True:


    conn, addr = s.accept()

    print('Got connection from', addr)

    message = conn.recv(1024)

    print("Client : " + message)

    conn.close()


米琪卡哇伊
浏览 31回答 1
1回答

摇曳的蔷薇

查看堆栈跟踪,我发现您正在尝试连接导致崩溃的 ui 线程。您需要将连接逻辑移至其自己的线程中这是可以帮助您的文档链接&nbsp;https://developer.android.com/guide/components/processes-and-threads#Threads尝试这个主要活动import android.Manifest;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Application;import android.content.ActivityNotFoundException;import android.content.ContentValues;import android.content.Intent;import android.content.SharedPreferences;import android.content.pm.PackageManager;import android.graphics.Bitmap;import android.location.Address;import android.location.Geocoder;import android.location.Location;import android.media.Image;import android.net.Uri;import android.os.AsyncTask;import android.os.Build;import android.os.Bundle;import android.provider.MediaStore;import android.provider.Settings;import android.speech.RecognitionListener;import android.speech.RecognizerIntent;import android.speech.SpeechRecognizer;import android.speech.tts.TextToSpeech;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.GridLayout;import android.widget.TextView;import com.chaquo.python.PyObject;import com.chaquo.python.Python;import com.chaquo.python.android.AndroidPlatform;import com.google.android.gms.location.FusedLocationProviderClient;import com.google.android.gms.tasks.OnCompleteListener;import com.google.android.gms.tasks.OnFailureListener;import com.google.android.gms.tasks.OnSuccessListener;import com.google.android.gms.tasks.Task;import com.google.firebase.ml.vision.FirebaseVision;import com.google.firebase.ml.vision.common.FirebaseVisionImage;import com.google.firebase.ml.vision.text.FirebaseVisionText;import com.google.firebase.ml.vision.text.FirebaseVisionTextDetector;import org.w3c.dom.Text;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.Random;import java.net.*;import java.io.*;import static android.Manifest.permission.ACCESS_FINE_LOCATION;import static android.Manifest.permission.CAMERA;import static android.Manifest.permission.READ_EXTERNAL_STORAGE;import static android.Manifest.permission.RECORD_AUDIO;import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; private static final String TAG = "MainActivity";&nbsp; &nbsp; private Button btnRecognize;&nbsp; &nbsp; private SpeechRecognizer speechRecognizer;&nbsp; &nbsp; static EditText ET_ShowRecognized;&nbsp; &nbsp; String locality;&nbsp; &nbsp; private Intent intent;&nbsp; &nbsp; private String ET_ShowRecognizedText;&nbsp; &nbsp; private String ProcessingText;&nbsp; &nbsp; //private FusedLocationProviderClient fusedLocationProviderClient;&nbsp; &nbsp; //Geocoder geocoder;&nbsp; &nbsp; Python py;&nbsp; &nbsp; PyObject pyobj;&nbsp; &nbsp; PyObject obj;&nbsp; &nbsp; String currentDate;&nbsp; &nbsp; String currentTime;&nbsp; &nbsp; static TextToSpeech tts;&nbsp; &nbsp; Uri imageURI;&nbsp; &nbsp; ContentValues contentValues;&nbsp; &nbsp; Intent cameraIntent;&nbsp; &nbsp; static final int REQUEST_IMAGE_CAPTURE = 1;&nbsp; &nbsp; Image mediaImage;&nbsp; &nbsp; FirebaseVisionImage firebaseVisionImage;&nbsp; &nbsp; static Bitmap&nbsp; imageBitmap;&nbsp; &nbsp; FirebaseVisionTextDetector textDetector;&nbsp; &nbsp; String imgText;&nbsp; &nbsp; Intent CameraIntent;&nbsp; &nbsp; static Thread sent;&nbsp; &nbsp; static Thread receive;&nbsp; &nbsp; static Socket socket;&nbsp; &nbsp; InputStreamReader in;&nbsp; &nbsp; BufferedReader bf;&nbsp; &nbsp; String ServerOutput;&nbsp; &nbsp; PrintWriter writer;&nbsp; &nbsp; String ServerInput;&nbsp; &nbsp; @SuppressLint({"SetTextI18n", "ClickableViewAccessibility", "MissingPermission"})&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, CAMERA}, PackageManager.PERMISSION_GRANTED);&nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized = findViewById(R.id.ET_ShowRecognized);&nbsp; &nbsp; &nbsp; &nbsp; btnRecognize = findViewById(R.id.btnRecognize);&nbsp; &nbsp; &nbsp; &nbsp; /*fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<Location> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Location location = task.getResult();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(location != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; geocoder = new Geocoder(MainActivity.this, Locale.getDefault());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locality = address.get(0).getLocality();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; if(!Python.isStarted()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Python.start(new AndroidPlatform(this));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; py = Python.getInstance();&nbsp; &nbsp; &nbsp; &nbsp; pyobj = py.getModule("WolframAlpha");&nbsp; &nbsp; &nbsp; &nbsp; obj = pyobj.callAttr("main", locality);*/&nbsp; &nbsp; &nbsp; &nbsp; tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onInit(int i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == TextToSpeech.SUCCESS) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.setLanguage(Locale.ENGLISH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("Hi you successfully ran me.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("Seems good to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; //currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());&nbsp; &nbsp; &nbsp; &nbsp; //currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());&nbsp; &nbsp; &nbsp; &nbsp; //textToSpeech.speak("Hi! I am your personal assistant. Today date is something something ", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; //Speak("Today's weather forecast for the current location is " + obj.toString());&nbsp; &nbsp; &nbsp; &nbsp; intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);&nbsp; &nbsp; &nbsp; &nbsp; speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);&nbsp; &nbsp; &nbsp; &nbsp; speechRecognizer.setRecognitionListener(new RecognitionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onReadyForSpeech(Bundle bundle) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onBeginningOfSpeech() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onRmsChanged(float v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onBufferReceived(byte[] bytes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onEndOfSpeech() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onError(int i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResults(Bundle bundle) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> mathches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mathches != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized.setText(mathches.get(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onPartialResults(Bundle bundle) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onEvent(int i, Bundle bundle) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; btnRecognize.setOnTouchListener(new View.OnTouchListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onTouch(View view, MotionEvent motionEvent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (motionEvent.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speechRecognizer.stopListening();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized.setText(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized.setText("Listening...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speechRecognizer.startListening(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public void process() {&nbsp; &nbsp; &nbsp; &nbsp; ProcessingText = ET_ShowRecognized.getText().toString().toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; if(ProcessingText.contains("hello")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("Hi! I hope all is well.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(ProcessingText.contains("hi")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("Hello! Nice to meet you.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(ProcessingText.contains("your name")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("My name is assistant.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(ProcessingText.contains("recognise text")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("Opening Camera.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatchTakePictureIntent();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(ProcessingText.contains("bye")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(ProcessingText.contains("current temperature")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendTemp();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recieve_data();&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak(ProcessingText, TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void dispatchTakePictureIntent() {&nbsp; &nbsp; &nbsp; &nbsp; CameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(CameraIntent, REQUEST_IMAGE_CAPTURE);&nbsp; &nbsp; &nbsp; &nbsp; } catch (ActivityNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // display error state to the user&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onActivityResult(int requestCode, int resultCode, Intent data) {&nbsp; &nbsp; &nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data);&nbsp; &nbsp; &nbsp; &nbsp; if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle extras = data.getExtras();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imageBitmap = (Bitmap) extras.get("data");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //imageView.setImageBitmap(imageBitmap);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; detectTextFromImage();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void detectTextFromImage() {&nbsp; &nbsp; &nbsp; &nbsp; firebaseVisionImage = FirebaseVisionImage.fromBitmap(imageBitmap);&nbsp; &nbsp; &nbsp; &nbsp; textDetector = FirebaseVision.getInstance().getVisionTextDetector();&nbsp; &nbsp; &nbsp; &nbsp; textDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(FirebaseVisionText firebaseVisionText) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //speakTextFromImage(firebaseVisionText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getImgText(firebaseVisionText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }).addOnFailureListener(new OnFailureListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressLint("SetTextI18n")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("Something went wrong. Please try again later or try with another image.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized.setText("Something went wrong. Please try again later or try with another image.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @SuppressLint("SetTextI18n")&nbsp; &nbsp; private void getImgText(FirebaseVisionText firebaseVisionText){&nbsp; &nbsp; &nbsp; &nbsp; List<FirebaseVisionText.Block> blockList = firebaseVisionText.getBlocks();&nbsp; &nbsp; &nbsp; &nbsp; if(blockList.size() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("I think this image contains no text.", TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized.setText("I think this image contains no text.");&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(FirebaseVisionText.Block block : firebaseVisionText.getBlocks()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgText = block.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tts.speak("The text in the image is as follows : " + imgText, TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ET_ShowRecognized.setText("The text in the image is as follows : " + imgText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void recieve_data(){&nbsp; &nbsp; &nbsp; &nbsp; ServerInput = "Java client is successfully connected with the server ";&nbsp; &nbsp; &nbsp; &nbsp; BackgroundTask bt = new BackgroundTask();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bt.execute(ServerInput);&nbsp; &nbsp; }&nbsp; &nbsp; public void sendTemp(){&nbsp; &nbsp; &nbsp; &nbsp; new TempBackgroundTask().execute();&nbsp; &nbsp; }&nbsp; &nbsp; class TempBackgroundTask extends AsyncTask<Void, String, Void>{&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Void doInBackground(String... voids) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket = new Socket("myIP",12345);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (UnknownHostException e1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e1.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e1.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sent = new Thread(new Runnable(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerOutput = bf.readLine().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; publishProgress(ServerOutput);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sent.start();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sent.join();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onProgressUpdate(String... text) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainActivity.tts.speak(text[0], TextToSpeech.QUEUE_FLUSH, null, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainActivity.ET_ShowRecognized.setText(text[0]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class BackgroundTask extends AsyncTask<String, Void, Void>{&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Void doInBackground(String... voids) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message = voids[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket = new Socket("192.168.43.203", 24224);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer = new PrintWriter(socket.getOutputStream());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.write(message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch (IOException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPause() {&nbsp; &nbsp; &nbsp; &nbsp; super.onPause();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python