I am doing an API call but the API sends back an error with the status code 404. How can I get the response of the API so I can see what the exact error is?
The error I am getting is:
08-12 09:52:05.977 11040-11221/eu.app W/RestTemplate﹕ POST request for "http://hidden-api-url.com" resulted in 404 (Not Found); invoking error handler
08-12 09:52:09.051 11040-11221/eu.app E/AndroidRuntime﹕ FATAL EXCEPTION: pool-5-thread-3
Process: eu.app, PID: 11040
org.springframework.web.client.HttpClientErrorException: 404 Not Found
I found the solution. I caught the HttpClientErrorException and got the response with getResponseBodyAsString();.
try {
restClient.restCall(data);
} catch (HttpClientErrorException e) {
Log.d("Response",e.getResponseBodyAsString());
}
Related
Getting this error in sample app.
java.net.ProtocolException: Expected HTTP 101 response but was '401
Not Authorized'
Using this code snippet to transalate.
TranslateOptions translateOptions = new TranslateOptions.Builder()
.addText(params[0])
.source(Language.ENGLISH)
.target(selectedTargetLanguage)
.build();
TranslationResult translationResult = translationService
.translate(translateOptions)
.execute();
Once it executes the application crashes and I see this error in logs.
D/OkHttp: --> POST https://gateway.watsonplatform.net/language-translator/api/v2/translate/v2/translate http/1.1 (46-byte body)
D/OkHttp: <-- 401 Not Authorized https://gateway.watsonplatform.net/language-translator/api/v2/translate/v2/translate (413ms, unknown-length body)
POST https://gateway.watsonplatform.net/language-translator/api/v2/translate/v2/translate, status: 401, error: Not Authorized
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.gobiggi.watsontutorial, PID: 10136
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by:
com.ibm.watson.developer_cloud.service.exception.UnauthorizedException: Unauthorized: Access is denied due to invalid credentials. Tip: Did you set the Endpoint?
at com.ibm.watson.developer_cloud.service.WatsonService.processServiceCall(WatsonService.java:410)
at com.ibm.watson.developer_cloud.service.WatsonService$1.execute(WatsonService.java:174)
at com.gobiggi.watsontutorial.MainActivity$TranslationTask.doInBackground(MainActivity.java:328)
at com.gobiggi.watsontutorial.MainActivity$TranslationTask.doInBackground(MainActivity.java:321)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
i don't know too much about how watson sdk works but just by looking at the error i see:
com.ibm.watson.developer_cloud.service.exception.UnauthorizedException:
Unauthorized: Access is denied due to invalid credentials. Tip: Did
you set the Endpoint?
Is saying the credentials presented are not valid. You should start by looking at that.
Am doing my project using retrofit2. When my Call goes on failure i repeat same call again.Repeation of this call made my app to force close.
When i look on log i got error log, which is given below. I felt this is caused by multiple enqueue to same Call. So i did that before enqueus i called cancel. But its not working. getting same force close.
FATAL EXCEPTION: main
Process: com.SocialMob, PID: 27846
java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again
at java.lang.VMThread.create(Native Method)
at java.lang.Thread.start(Thread.java:1029)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:920)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1338)
at okhttp3.Dispatcher.enqueue(Dispatcher.java:112)
at okhttp3.RealCall.enqueue(RealCall.java:78)
at okhttp3.RealCall.enqueue(RealCall.java:70)
at retrofit2.OkHttpCall.enqueue(OkHttpCall.java:104)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.enqueue(ExecutorCallAdapterFactory.java:58)
at com.SocialMob.Activities.SplashActivity.VersionCheck(SplashActivity.java:184)
at com.SocialMob.Activities.SplashActivity.access$500(SplashActivity.java:38)
at com.SocialMob.Activities.SplashActivity$1.onFailure(SplashActivity.java:177)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$2.run(ExecutorCallAdapterFactory.java:75)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Thanks in advance.
You should avoid that approach, as it would make it a recursive call. Instead you should check the cause in the failure function first and then retry. Also fix the number of retry times.
I am using Retrofit 2.0.2, and i have this tag in my manifest file:
android:largeHeap="true"
I am retrying on failure like this:
#Override
public void onFailure(Call<AudioResponse> call, Throwable error) {
loading.setVisibility(View.GONE);
if (mAdapter.getItemCount() == 0) {
SetErrorContent();
}
Log.e("Error", error.getMessage() + "");
call.cancel();
call.clone().enqueue(this);
}
It is not crashing.Give it a try.
how to add username and password in http request to call web service? I have write REST web service and if user want to do request ,he neeed to request like this in terminal :
curl -u username:password http://localhost:8080/user/folder
httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1),
new UsernamePasswordCredentials("kyaw", "password"));
get.setHeader("Authorization","Basic a31hdzpwYXNzd29yZA==");
I used either way to do in android but get exception :
05-20 07:13:30.900: DEBUG/AndroidRuntime(557): Shutting down VM
05-20 07:13:30.900: WARN/dalvikvm(557): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-20 07:13:30.972: ERROR/AndroidRuntime(557): FATAL EXCEPTION: main
05-20 07:13:30.972: ERROR/AndroidRuntime(557): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.put}: java.lang.RuntimeException: Binary XML file line #18: You must supply a layout_width attribute.
05-20 07:13:30.972: ERROR/AndroidRuntime(557): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-20 07:13:30.972: ERROR/AndroidRuntime(557): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
How do i do?
The error is "You must supply a layout_width attribute" (copy pasted from your stack trace).
The error is unrelated to the code you are showing. You are missing a layout_width="" attribute in one of your components XML declaration.
I am trying to parse the xml data coming from webservice using SAX parser, when i try to parse the data(size:7.4MB) using URL, it works fine but when I copy the xml data from the URL and place the xml file(size:7.4MB) in the raw folder, the parsing fails.
When the size of file placed in raw folder is reduced to 1.95MB, it works fine. I have tried shuffling the tags to check if there are any issues with the data, I haven't found any.It works fine as long as the size doesn't exceed 1.95MB. Is there any size limit while parsing the data from raw folder in android? if yes what is the limit? or How to parse the large files using SAX parser?
Any pointers will really help me. Thanks in advance for your time and any help offered.
Regards,
Ramesh
02-14 17:35:35.651: DEBUG/asset(11035): Data exceeds UNCOMPRESS_DATA_MAX (2138421 vs 2097152)
02-14 17:35:35.651: WARN/dalvikvm(11035): threadid=9: thread exiting with uncaught exception (group=0x4001d7d0)
02-14 17:35:35.659: ERROR/AndroidRuntime(11035): FATAL EXCEPTION: Thread-13
02-14 17:35:35.659: ERROR/AndroidRuntime(11035): java.lang.AssertionError: java.io.IOException
02-14 17:35:35.659: ERROR/AndroidRuntime(11035): at android.util.Xml.parse(Xml.java:89)
code
try {
System.out.println("1");
//Xml.parse((url.openConnection().getInputStream()), Encoding.UTF_8, datahandler);
Xml.parse(getResources().openRawResource(R.raw.sample), Encoding.UTF_8, datahandler);
System.out.println("2");
//xr.parse(new InputSource(getAssets().open("sample.xml")));
}catch(Exception e){
System.out.println("Ramesh"+"XML Parsing exception:"+e);
}
error
Android DEBUG/asset(11035): Data exceeds UNCOMPRESS_DATA_MAX (2138421 vs 2097152)
WARN/dalvikvm(11035): threadid=9: thread exiting with uncaught exception (group=0x4001d7d0)
ERROR/AndroidRuntime(11035): FATAL EXCEPTION: Thread-13
ERROR/AndroidRuntime(11035): java.lang.AssertionError: java.io.IOException
ERROR/AndroidRuntime(11035): at android.util.Xml.parse(Xml.java:89)
ERROR/AndroidRuntime(11035): at com.infosys.gss.ui.Login$2$1.run(Login.java:431)
ERROR/AndroidRuntime(11035): Caused by: java.io.IOException
I'm receiving a null pointer execption at the end of a SFTP session.
What am I missing ?
This the error :
09-05 16:38:05.844 9298-9307/? E/System: Uncaught exception thrown by finalizer
09-05 16:38:05.844 9298-9307/? E/System: java.lang.NullPointerException
at com.enterprisedt.net.j2ssh.sftp.SftpFileOutputStream.finalize(SftpFileOutputStream.java:203)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
at java.lang.Thread.run(Thread.java:841)
NO errors from the library during the SFTP session.
Basically I log in to the SFTP server calling these functions :
boolean success = sftp.Connect(hostname,22);
..........
CkSshKey key = new CkSshKey();
..........
String privKey = key.loadText(keypath);
..........
key.put_Password(password);
...........
success = key.FromOpenSshPrivateKey(privKey);
...........
success = sftp.AuthenticatePk(mPrefs.getSftpSapUser(),key);
Then I send the file :
Boolean success = sftp.InitializeSftp();
.............
success = sftp.UploadFileByName(remoteFilePath,localFilePath);
.............
sftp.SetPermissions(remoteFilePath, false, 511);
What am I missing ?
Thanks
This is not a crash in Chilkat. Look closely at the exception information:
com.enterprisedt.net.j2ssh.sftp.SftpFileOutputStream
The crash is in another software vendor's class (EnterpriseDT). Or more accurately: It might be a crash caused by incorrect usage of that vendor's class. It's not necessarily a defect in that vendor's software. (I really don't know..)