Can someone debug this , this is a This is an application using OpenCV library to recognize whats in the frame, It captures frames from the camera and sends them to a server at the IP address via an HTTP post request. The server then processes the frame and sends a response back to the application. The application uses the OkHttp to handle the HTTP requests. but in my case its not sending any data
package com.amore.fsltranslator;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import org.opencv.android.CameraActivity;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class NurseryRecognition extends CameraActivity {
CameraBridgeViewBase cameraBridgeViewBase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nursery_recognition);
/*OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://url/").build();
okHttpClient.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(#NonNull Call call, #NonNull IOException e) {
Toast.makeText(NurseryRecognition.this, "network not found",Toast.LENGTH_LONG).show();
}
#Override
public void onResponse(#NonNull Call call, #NonNull Response response) throws IOException {
}
});*/
if (OpenCVLoader.initDebug()) Log.d("Loaded", "success");
else Log.d("Loaded", "error");
//cameraPermission
getPermission();
cameraBridgeViewBase = findViewById(R.id.cameraView);
cameraBridgeViewBase.setCvCameraViewListener(new CameraBridgeViewBase.CvCameraViewListener2() {
long previousTime = System.currentTimeMillis();
#Override
public void onCameraViewStarted(int width, int height) {
}
#Override
public void onCameraViewStopped() {
}
#Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
long currentTime = System.currentTimeMillis();
Mat frame = inputFrame.rgba();
if (currentTime - previousTime >= 1000) {
previousTime = currentTime;
// code to send the frame
sendFrameToServer(frame);
}
return frame;
}
private void sendFrameToServer(Mat frame) {
// Convert the frame to a format that can be sent over the network
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", frame, matOfByte);
byte[] byteArray = matOfByte.toArray();
RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), byteArray);
// Create the OkHttp client
OkHttpClient client = new OkHttpClient();
// Create the request
Request request = new Request.Builder()
.url("http://url/load_img/")
.post(requestBody)
.build();
// Send the request
client.newCall(request).enqueue(new Callback() {
#Override
public void onResponse(#NonNull Call call, #NonNull Response response) throws IOException {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(NurseryRecognition.this, "YEEEEY!", Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(NurseryRecognition.this, "Ney :(", Toast.LENGTH_LONG).show();
}
});
}
});
}
});
if(OpenCVLoader.initDebug()){
cameraBridgeViewBase.enableView();
}
}
#Override
protected void onResume() {
super.onResume();
cameraBridgeViewBase.enableView();
}
#Override
protected void onDestroy() {
super.onDestroy();
cameraBridgeViewBase.disableView();
}
#Override
protected void onPause() {
super.onPause();
cameraBridgeViewBase.disableView();
}
#Override
protected List<? extends CameraBridgeViewBase> getCameraViewList() {
return Collections.singletonList(cameraBridgeViewBase);
}
void getPermission(){
if(checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.CAMERA}, 101);
}
}
#Override
public void onRequestPermissionResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length>0 && grantResults[0]!=PackageManager.PERMISSION_GRANTED) {
getPermission();
}
}
}
is it okay to use okhttp ? or should i use retrofit ? im not good in programming sorry for noob question.
Related
I am trying to use socket.io to connect to the Nodejs server and Android client. I am getting the xhr poll error when trying to connect. I have seen such issues raised by other people, but with no solution. I have socket io 2.1.1 on node server and using 'io.socket:socket.io-client:0.8.3' in Android. My Android version is Oreo.
This is my Android client code:
package com.example.socketiodemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.socket.client.IO;
import io.socket.client.Manager;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends AppCompatActivity {
private TextView display_text;
private Socket mSocket;
Manager mManager;
private boolean fClientClosedConnection;
private boolean fClientIsConnected;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
display_text = (TextView)findViewById(R.id.display_text);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
fClientClosedConnection = false;
fClientIsConnected = false;
new MyAsyncTask(mSocket).execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyAsyncTask extends AsyncTask<String, Void, String> {
Socket mSocket;
Manager mManager;
MyAsyncTask(Socket socket) {
mSocket = socket;
}
#Override
protected String doInBackground(String... params) {
Log.e(TAG, "Initiating the connection");
try {
mSocket = IO.socket("http://192.168.0.104:4444");
mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onTimeoutError);
mSocket.on(Socket.EVENT_ERROR, onError);
mSocket.on("data", onNewMessage);
mSocket.connect();
while (!fClientClosedConnection) {
// Keep the thread active
}
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
Log.e(TAG, "Finished the background Process");
return "";
}
#Override
protected void onPostExecute(String result) {
mSocket.close();
}
}
private Emitter.Listener onConnect = new Emitter.Listener() {
#Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e(TAG, "onConnect");
if(!fClientIsConnected) {
Toast.makeText(getApplicationContext(),
"Client is connected", Toast.LENGTH_LONG).show();
fClientIsConnected = true;
}
}
});
}
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
#Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e(TAG, "diconnected");
fClientIsConnected = false;
Toast.makeText(getApplicationContext(),
"Client is disconnected", Toast.LENGTH_LONG).show();
}
});
}
};
private Emitter.Listener onConnectError = new Emitter.Listener() {
#Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e(TAG, "Error connecting: " + args[0].toString());
Toast.makeText(getApplicationContext(),
"Client connect error", Toast.LENGTH_LONG).show();
}
});
}
};
private Emitter.Listener onTimeoutError = new Emitter.Listener() {
#Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e(TAG, "Timeout Error ");
}
});
}
};
private Emitter.Listener onError = new Emitter.Listener() {
#Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.e(TAG, "Error ");
}
});
}
};
private Emitter.Listener onNewMessage = new Emitter.Listener() {
#Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
#Override
public void run() {
JSONObject data = (JSONObject) args[0];
String message;
try {
message = data.getString("message");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
}
printMessage(message);
if (message.contentEquals("Bye"))
{
fClientClosedConnection = true;
}
}
});
}
};
private void printMessage (String message)
{
display_text.setText(message);
}
}
This is my nodejs server:
process.title = 'node-android';
var app = require('http').createServer()
var io = require('socket.io')(app);
app.listen(4444, onListenStart);
io.on('connection', onConnect);
function onListenStart()
{
console.log("Server listening on port: " + SERVER_PORT);
}
function onConnect(client)
{
console.log("client is connected");
client.emit("data", { message: 'Hello'});
client.on('event', onMessageReceived);
client.on('disconnect', onDisconnect);
}
function onMessageReceived(data)
{
console.log(data);
}
function onDisconnect()
{
console.log("client is disconnected");
}
I get these logs:
05-20 21:48:56.404 28026-28026/com.example.socketiodemo E/MainActivity: Error connecting: io.socket.engineio.client.EngineIOException: xhr poll error
05-20 21:48:57.921 28026-28026/com.example.socketiodemo E/MainActivity: Error connecting: io.socket.engineio.client.EngineIOException: xhr poll error
Please help, as I have been stuck on this since 3 days.
I have socket io 2.1.1 on node server and using 'io.socket:socket.io-client:0.8.3'
I had similar situation, changing library version to 1.0.0 helped.
you must add this code in application tag in manifest like below,
It solved Error connecting: io.socket.engineio.client.EngineIOException: xhr poll error
same issues
<Application . . android:usesCleartextTraffic="true"> . . . <Application/>
I'm currently try to implement a two factor authentication system on a project i'm working on using twilio as a sms gateway service to request a random login token and then send it to the user as a text message. I followed the tutorial found here "https://www.twilio.com/blog/2016/05/how-to-send-an-sms-from-android.html" to test the service out. Following the tutorial I hosted the backend on Heroku. The app works just fine and says that the sms has been sent. However I never receive it. Any help would great.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private EditText mTo;
private EditText mBody;
private Button mSend;
private OkHttpClient mClient = new OkHttpClient();
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTo = (EditText) findViewById(R.id.txtNumber);
mBody = (EditText) findViewById(R.id.txtMessage);
mSend = (Button) findViewById(R.id.btnSend);
mSend.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
try {
post(" https://cryptic-shore-79857.herokuapp.com", new
Callback(){
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response)
throws IOException {
runOnUiThread(new Runnable() {
#Override
public void run() {
mTo.setText("");
mBody.setText("");
Toast.makeText(getApplicationContext(),"SMS Sent!",Toast.LENGTH_SHORT).show();
}
});
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
mContext = getApplicationContext();
}
Call post(String url, Callback callback) throws IOException {
RequestBody formBody = new FormBody.Builder()
.add("To", mTo.getText().toString())
.add("Body", mBody.getText().toString())
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Call response = mClient.newCall(request);
response.enqueue(callback);
return response;
}
}
I'm thinking the URL that connects to Heroku is incorrect but I have no idea what it should be.
Twilio developer evangelist here.
You're POSTing your request to the wrong URL. Currently your code does:
try {
post("https://cryptic-shore-79857.herokuapp.com", new
Callback(){
But the path for the action that sends the SMS should be:
try {
post("https://cryptic-shore-79857.herokuapp.com/sms", new
Callback(){
Note, the /sms path.
Let me know if that helps at all.
I wanna to save the result from OnResponse method to use it for updating the UI
i tried to save the result into String var then call it into main thread but it doesn't work .
here's my code with some comments ,
any help ?
package com.example.blacknight.testokhttp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static android.R.string.ok;
public class MainActivity extends AppCompatActivity {
public final String URL_MOVIE = "http://api.themoviedb.org/3/movie/popular?api_key=" + API_KEY;
String res_120 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL_MOVIE)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
}
#Override
public void onResponse(Call call, Response response) throws IOException {
Log.v("BK-201 URL: " , response.body().string());
// wanna save the result to update UI
res_120 = response.body().string();
}
});
// just for test : if the result has been saved or not
Log.i("BK-111 : " , res_120);
}
}
Let's say you want to update a TextView element in you UI with the response in a String format. You could do something like this. I keeped your test log to help you follow the code, just in case.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL_MOVIE)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
}
#Override
public void onResponse(Call call, Response response) throws IOException {
Log.v("BK-201 URL: " , response.body().string());
// wanna save the result to update UI
res_120 = response.body().string();
updateUI(response.body().string());
}
});
}
void updateUI(String string) {
textView.setText(string);
Log.i("BK-111 : " , res_120);
}
Here's a working code for anyone have the same problem or new on using OkHttp , Unfortunately i'm using AsyncTask
Thaks to Jofre Mateu
package com.example.blacknight.testokhttp;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
public final String URL_MOVIE = "http://api.themoviedb.org/3/movie/popular?api_key=" + API_KEY ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MovieTask().execute();
}
public class MovieTask extends AsyncTask<String , Void , String>
{
#Override
protected String doInBackground(String... params) {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL_MOVIE)
.build();
Response response = client.newCall(request).execute();
String res_120 = response.body().string();
return res_120;
} catch (Exception e ){
return null;
}
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
TextView textView = (TextView) findViewById(R.id.testView_test);
textView.setText(s);
}
}
}
I am trying to read the httpstatus code e body in the success case of a request. So I created the code below to test but I failed to get the onNext called, I tried to use the okhttp (com.squareup.okhttp.Response) and retrofit Response (retrofit.Response) class, but I couldn't make it work.
Can someone help me to read the body and httpstatus code here? I would like to keep using the Observables.
Thanks in advance.
package com.app.rest;
import com.squareup.okhttp.Response;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import retrofit.http.GET;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class TestApiClient {
public interface Test {
#GET("/posts")
Observable<Response> getPosts();
}
public TestApiClient() {
new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build().create(Test.class).getPosts().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Response>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
e.toString();
}
#Override
public void onNext(Response response) {
response.toString();
}
});
}
}
I got the answer.
import com.squareup.okhttp.ResponseBody;
import retrofit.Response;
...
public Observable<Response<ResponseBody>> xxx(){}
...
Playlist playlist = Playlist.parse(((ResponseBody)response.body()).byteStream());
Actually, response.body() is a Object, You can cast it to another type.In this case, it is ResponseBody.
I got it working with the code below, it was to needed to use the generics of retrofit.Response with ResponseBody:
package com.app.rest;
import com.squareup.okhttp.ResponseBody;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import retrofit.http.GET;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class TestApiClient {
public interface Test {
#GET("/posts")
Observable<Response<ResponseBody>> getPosts();
}
public TestApiClient() {
new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build().create(Test.class).getPosts().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Response<ResponseBody>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
e.toString();
}
#Override
public void onNext(Response<ResponseBody> response) {
response.toString();
}
});
}
}
You need to define your interface's Observable as
#GET("/posts")
Observable<Result<MyPost>>
Then you can easily get the response body in onNext() like this:
#Override
public void onNext(Result<MyPost> result) {
result.response().body();
}
Sometimes the response.toString() or response.body() gives you unreadable string, I had to get the bytes from the response body and construct a new string from it.
#Override
public void onNext(Response<ResponseBody> response) {
try {
String responseStr = new String(response.body().bytes());
Log.d("responseBodyResponse", responseStr);
} catch (Exception e) {
Log.d("responseBodyResponse Exception", e.getMessage());
}
}
I want to make a library to reduce my duplicate network works on every android projects or even give my jar to some other developers to using my methods for network communications.
So i build this:
import java.util.Map;
import org.json.JSONObject;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
public class RequestResp {
private final static String WEB_SERVICE_URL = "http://blabla/api";
private final Priority priorityImmediatelly = Priority.IMMEDIATE;
private final Priority priorityHigh = Priority.HIGH;
private final Priority priorityNORMAL = Priority.NORMAL;
private String tag_req_default = "tag_req_default";
VolleyCustomRequest mVolleyCustomReq;
DefaultRetryPolicy drp = new DefaultRetryPolicy(15000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
public /*JSONObject*/ void sendParamsAsHighPriority(Map<String, String> params) {
mVolleyCustomReq = new VolleyCustomRequest(Method.POST,
WEB_SERVICE_URL, params, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
if (response != null) {
}
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(tag_req_default, error.getMessage());
}
}) {
#Override
public Priority getPriority() {
return priorityHigh;
}
};
mVolleyCustomReq.setRetryPolicy(drp);
VolleyController.getInstance().addToRequestQueue(mVolleyCustomReq,
tag_req_default);
/*return response; ?!?!?*/
}
}
But how to return response?! Cause if server was busy or down or something that make response a little late, developers in their applications get null!(i guess).
How to make a such this?! Build a jar library that has a class that has a method that give parameters and send it on specific URL, with volley library?
Define Interface like
public interface OntaskCompleted {
public void onSuccess(JSONObject response);
public void onError(String message);
}
Now Your activity should implement this interface and you have to override these method.
Now in you Volley class do this.
if (response != null) {
ontaskCompleted.onSuccess(JSONObject);
}
and
public void onErrorResponse(VolleyError error) {
VolleyLog.d(tag_req_default, error.getMessage());
ontaskCompleted.onError( error.getMessage());
}
Now your activity will get the result of error or success.
Hope it helps you.