I'm using reactive for android for my request to backend, the problem is that the request sometimes work and sometimes not.
and i get the exception
io.reactivex.exceptions.UndeliverableException: android.os.NetworkOnMainThreadException
in Mainactivity onCreate() i'm calling GetServices function which sends a post request
Get services
public Observable<Response> GetService() {
return Observable.fromCallable(new Callable<Response>() {
#Override
public Response call() throws Exception {
return RequestHelper.GetRequest("getvendoractiveservices");
}
});
}
public void GetServices()
{
GetService().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Response>() {
#Override
public void onSubscribe(#io.reactivex.annotations.NonNull Disposable d) {
}
#Override
public void onNext(#io.reactivex.annotations.NonNull Response value) {
try {
Log.w("services",value.body().string()); //exception here
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onError(#io.reactivex.annotations.NonNull Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
});
}
my postrequest
public static String URI="http://192.168.137.1:4000/";
public static Response PostRequest(String api, FormBody formBody)
{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URI+api)
.header("carmen-token", LoginActivity.token)
.post(formBody)
.build();
try {
return client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Related
I'm building an app that calls to a webservice. When i use HttpUrlConnection class to make a request to the webservice then it works, but i replace with OkHttpClient to make request then my app can't connect to the service, OnFailure method is always called with content: "OnFailure: failed to connect to todolistmobileapp-env.ap-south-1.elasticbeanstalk.com/13.232.26.212 (port 80) after 4ms"
Anyone tell me what problem i'm getting into?
public class RequestRegisterAuthor extends AppNetworkRequest {
public static final String TAG = RequestRegisterAuthor.class.getSimpleName();
private final String url = ToDoRestAPIs.baseRemoteHostUrl + ToDoRestAPIs.registerAuthor;
public RequestRegisterAuthor(APICallbackListener apiCallbackListener, Object jsonRequestBody){
super(apiCallbackListener);
request = new Request.Builder().url(url)
.addHeader(AppNetworkRequest.CONTENT_TYPE, AppNetworkRequest.JSON_CONTENT_TYPE)
.post(RequestBody.create(MediaType.parse(JSON_CONTENT_TYPE), jsonRequestBody.toString()))
.build();
}
#Override
public void makeBackEndRequest() {
new Thread(this).start();
}
#Override
public void run() {
okHttpClient.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call,final IOException e) {
handler.post(new Runnable() {
#Override
public void run() {
apiCallbackListener.onFailureCallback(e.getMessage());
Log.e(TAG + "- OnFailure", e.getMessage());
}
});
}
#Override
public void onResponse(Call call, Response response) throws IOException {
try {
if(response.code() == 201){
responseObject = new GsonBuilder().create().fromJson(response.body().string(), Author.class);
}
else{
responseObject = new Error(response.code(), response.message());
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
responseObject = new Error(101, e.getMessage()); // pre-defined code.
}
handler.post(new Runnable() {
#Override
public void run() {
apiCallbackListener.onSuccessCallback(REQUEST_TYPE.REQUEST_REGISTER_AUTHOR, responseObject);
}
});
}
});
} }
Here is the logcat:
https://pastebin.com/3CNsGrPm
I believe HttpUrlConnection uses OkHttp under the hood, could you post your code for both methods of connecting?
I want to make this request in a fragment but the errors, as could I do?, thanks! I have many errors as .Builder() or OkHttpClient() when I want to implement it in a Fragment so I do not say any error in particular
String url = "https://freegeoip.net/json/"
Request mRequest = new Request.Builder()
.url(url)
.build();
Context mContext // A UI context
new OkHttpClient().newCall(mRequest).enqueue(new Callback() {
Handler mainHandler = new Handler(mContext.getApplicationContext().getMainLooper());
#Override
public void onResponse(Call call, Response response) {
String responseBody = null;
try {
responseBody = response.body().string();
} catch (final IOException e) {
mainHandler.post(new Runnable() {
#Override
public void run() {
// handle error
}
});
}
final String finalResponseBody = responseBody;
mainHandler.post(new Runnable() {
#Override
public void run() {
// handle response body
try {
JSONObject locationObject = new JSONObject(finalResponseBody);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onFailure(Call call, final IOException e) {
mainHandler.post(new Runnable() {
#Override
public void run() {
mNetworkListener.onNetworkError(e.getMessage());
}
});
}
});
I am implementing Helper class in Android studio to service Activity
public void getLastId()
{
//init OkHttpClient
OkHttpClient client = new OkHttpClient();
//backend url
Request request = new Request.Builder()
.url("http://192.168.1.102:8080/aquabackend/public/customers/lastid")
.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 {
String jsonData = response.body().string();
try {
JSONObject jobject = new JSONObject(jsonData);
String id = jobject.getString("id");
//increment current id +1
String last_id = String.valueOf(Integer.parseInt(id)+1);
Log.i("new id", last_id);
} catch (Exception e) {
e.printStackTrace();
}
//Log.i("ok", response.body().string());
}
});
My function call in activity class
Helper helper = new Helper();
helper.getLastId();
//I want to get method to return lastId and then manipulate with the data
How can I make method return value of the id?
As it is an asynchronous process you won't be able to return a value from the method itself. However, you can use a callback to provide you the value when the asynchronous process has been completed. Below is an example of how you might want to do this.
public interface GetLastIdCallback {
void lastId(String id);
}
You would modify getLastId as follows:
public void getLastId(GetLastIdCallback idCallback) {
...
String last_id = String.valueOf(Integer.parseInt(id)+1);
idCallback.lastId(last_id);
...
}
Your Helper class usage would now look like this:
Helper helper = new Helper();
helper.getLastId(new GetLastIdCallback() {
#Override
public void lastId(String id) {
// Do something with your id
}
});
I'd suggest making your callback a bit more generic than I have suggested above. It could look like this:
public interface GenericCallback<T> {
void onValue(T value);
}
...
Helper helper = new Helper();
helper.getLastId(new GenericCallback<String>() {
#Override
public void onValue(String value) {
// Do something
}
});
If you used an interface like above you would be able to work with any return type.
Create a interface.
public interface Result{
void getResult(String id);
}
Now, pass interface to method as parameter.
Helper helper = new Helper();
helper.getLastId(new Result(){
#Override
void getResult(String id){
}
});
And In your method :
public void getLastId(final Result result)
{
//init OkHttpClient
OkHttpClient client = new OkHttpClient();
//backend url
Request request = new Request.Builder()
.url("http://192.168.1.102:8080/aquabackend/public/customers/lastid")
.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 {
String jsonData = response.body().string();
try {
JSONObject jobject = new JSONObject(jsonData);
String id = jobject.getString("id");
//increment current id +1
String last_id = String.valueOf(Integer.parseInt(id)+1);
Log.i("new id", last_id);
result.getResult(last_id);
} catch (Exception e) {
e.printStackTrace();
}
//Log.i("ok", response.body().string());
}
});
You have to create interface for it.
public interface getResponse {
void getJsonResponse(final String id);
}
In Your code :
public void getLastId(fianl getResponse response)
{
//init OkHttpClient
OkHttpClient client = new OkHttpClient();
//backend url
Request request = new Request.Builder()
.url("http://192.168.1.102:8080/aquabackend/public/customers/lastid")
.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 {
String jsonData = response.body().string();
try {
JSONObject jobject = new JSONObject(jsonData);
String id = jobject.getString("id");
//increment current id +1
String last_id = String.valueOf(Integer.parseInt(id)+1);
Log.i("new id", last_id);
if(response!=null){
response.getJsonResponse(last_id)
}
} catch (Exception e) {
e.printStackTrace();
}
//Log.i("ok", response.body().string());
}
});
In Activity :
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Helper helper = new Helper();
helper.getLastId(new getResponse(){
#Override
void getJsonResponse(String id){
}
});
}}
}
Here is the scenario: I have an Activity, named MainActivity, calling a OkHttp wrapper class, named NetworkManager to perform network post in background:
// In MainActivity
NetworkManager manager = new NetworkManager();
try {
manager.post("http://www.example.com/api/", reqObj); // reqObj is a JSONObject
} catch(IOException ioe) {
Log.e(TAG, ioe.getMessage());
}
Then, in the NetworkManager, I perform the POST action in asynchronous mode:
public class NetworkManager {
static String TAG = "NetworkManager";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, JSONObject json) throws IOException {
//RequestBody body = RequestBody.create(JSON, json);
try {
JSONArray array = json.getJSONArray("d");
RequestBody body = new FormEncodingBuilder()
.add("m", json.getString("m"))
.add("d", array.toString())
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
// Asynchronous Mode
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Request request, IOException e) {
Log.e(TAG, e.toString());
// what should I put here?
}
#Override
public void onResponse(Response response) throws IOException {
Log.w(TAG, response.body().string());
// what should I put here?
}
});
} catch (JSONException jsone) {
Log.e(TAG, jsone.getMessage());
}
}
}
What I'm trying to achieve is to call a function in MainActivity after network POST is successful or failed. How can I achieve this?
You can create an interface with onFailure and onResponse then let YourActivity implement it. And, on NetworkManagertry to notify YourActivity using listener.
// MainActivity implements NetworkListener
NetworkManager manager = new NetworkManager();
manager.setOnNetWorkListener(this);
try {
manager.post("http://www.example.com/api/", reqObj); // reqObj is a JSONObject
} catch(IOException ioe) {
Log.e(TAG, ioe.getMessage());
}
void onFailure(Request request, IOException e) {
// call your activity methods
}
void onResponse(Response response) {
// call your activity methods
}
// ======NetworkManager class============
public class NetworkManager {
static String TAG = "NetworkManager";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
public NetworkListenerv listener;
public void setOnNetWorkListener(NetworkListener listener) {
this.listener = listener
}
// Asynchronous Mode
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Request request, IOException e) {
Log.e(TAG, e.toString());
// what should I put here?
if (listener != null) {
listener.onFailure(request, e);
}
}
#Override
public void onResponse(Response response) throws IOException {
Log.w(TAG, response.body().string());
// what should I put here?
if (listener != null) {
listener.onResponse(response);
}
}
});
// Your interface;
public interface NetworkListener {
void onFailure(Request request, IOException e);
void onResponse(Response response);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NetworkOnMainThreadException
For a long time, I've been using generic code that does http requests in an AsyncTask. The AsyncTask returns an HttpResponse object. Everything worked great and the GUI thread never froze or anything.
Now, suddenly, this creates a NetworkOnMainThreadException:
serverResponse.getEntity().getContent();
What the heck?? Why is getEntity() considered networking?? In my mind, that line merely converts a response to an inputstream and should not need a network connection. Who made this decision? WHY did they decide this should be networking?
The async task:
public class AsyncHttpTask extends AsyncTask<HttpRequestInfo, Integer, HttpRequestInfo> {
public AsyncHttpTask() {
super();
}
protected HttpRequestInfo doInBackground(HttpRequestInfo... params) {
HttpRequestInfo rinfo = params[0];
try{
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(rinfo.getRequest());
rinfo.setResponse(resp);
}
catch (Exception e) {
rinfo.setException(e);
}
return rinfo;
}
#Override
protected void onPostExecute(HttpRequestInfo rinfo) {
super.onPostExecute(rinfo);
rinfo.requestFinished();
}
Callback interface:
public interface HttpCallback {
public void onResponse(HttpResponse serverResponse);
public void onError(Exception e);
}
HttpRequestInfo:
public class HttpRequestInfo {
private HttpUriRequest request_;
private HttpCallback callback_;
private Exception exception_;
private HttpResponse response_;
public HttpRequestInfo(HttpUriRequest request, HttpCallback callback) {
super();
request_ = request;
callback_ = callback;
}
public HttpUriRequest getRequest() {
return request_;
}
public void setRequest(HttpUriRequest request) {
request_ = request;
}
public HttpCallback getCallback() {
return callback_;
}
public void setCallback(HttpCallback callback) {
callback_ = callback;
}
public Exception getException() {
return exception_;
}
public void setException(Exception exception) {
exception_ = exception;
}
public HttpResponse getResponse() {
return response_;
}
public void setResponse(HttpResponse response) {
response_ = response;
}
public void requestFinished(){
if(exception_ != null){
callback_.onError(exception_);
}
else {
callback_.onResponse(response_);
}
}
}
Then I use jackson to convert the json response to an object. That's this is where the exception occurs:
#Override
public <T> T handleResponse(HttpResponse serverResponse, Class<T> typeOfResponse) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
T responseObject = null;
try {
responseObject = mapper.readValue(serverResponse.getEntity().getContent(),typeOfResponse); //THIS LINE IS EVIL
} catch (JsonParseException e) {
throw new ARException("Couldn't handle the response because the http response contained malformed json.",e);
} catch (JsonMappingException e) {
throw new ARException("Mapping the json response to the response object " + typeOfResponse + " failed.",e);
} catch (IllegalStateException e) {
throw new ARException("Couldn't convert the http response to an inputstream because of illegal state.",e);
} catch (IOException e) {
throw new ARException("Couldn't convert the http response to an inputstream.",e);
}
return responseObject;
}
Because you must work with network in separate thread and not main. You can use AsyncTask or Thread + Handler. If you are using AsyncTask all work with network you must perform in doInBackground part.