Add Header Parameter in Retrofit

Try this type header for Retrofit 1.9 and 2.0. For the JSON content type. @Headers({“Accept: application/json”}) @POST(“user/classes”) Call<playlist> addToPlaylist(@Body PlaylistParm parm); You can add many more headers, i.e, @Headers({ “Accept: application/json”, “User-Agent: Your-App-Name”, “Cache-Control: max-age=640000” }) Dynamically add to headers: @POST(“user/classes”) Call<ResponseModel> addToPlaylist(@Header(“Content-Type”) String content_type, @Body RequestModel req); Call your method, i.e., mAPI.addToPlayList(“application/json”, playListParam); Or … Read more

Getting java.net.SocketTimeoutException: Connection timed out in android

I’ve searched all over the web and after reading lot of docs regarding connection timeout exception, the thing I understood is that, preventing SocketTimeoutException is beyond our limit. One way to effectively handle it is to define a connection timeout and later handle it by using a try-catch block. Hope this will help anyone in … Read more

Read error response body in Java

Here is the bug report (close, will not fix, not a bug). Their advice there is to code like this: HttpURLConnection httpConn = (HttpURLConnection)_urlConnection; InputStream _is; if (httpConn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) { _is = httpConn.getInputStream(); } else { /* error from server */ _is = httpConn.getErrorStream(); }

HttpURLConnection Invalid HTTP method: PATCH

There are a lot of good answers, so here is mine (not work in jdk12): import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; public class SupportPatch { public static void main(String… args) throws IOException { allowMethods(“PATCH”); HttpURLConnection conn = (HttpURLConnection) new URL(“http://example.com”).openConnection(); conn.setRequestMethod(“PATCH”); } private static void … Read more

POST request send JSON data Java HttpUrlConnection

Your JSON is not correct. Instead of JSONObject cred = new JSONObject(); JSONObject auth=new JSONObject(); JSONObject parent=new JSONObject(); cred.put(“username”,”adm”); cred.put(“password”, “pwd”); auth.put(“tenantName”, “adm”); auth.put(“passwordCredentials”, cred.toString()); // <– toString() parent.put(“auth”, auth.toString()); // <– toString() OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream()); wr.write(parent.toString()); write JSONObject cred = new JSONObject(); JSONObject auth=new JSONObject(); JSONObject parent=new JSONObject(); cred.put(“username”,”adm”); cred.put(“password”, “pwd”); auth.put(“tenantName”, “adm”); … Read more

HTTPURLConnection Doesn’t Follow Redirect from HTTP to HTTPS

Redirects are followed only if they use the same protocol. (See the followRedirect() method in the source.) There is no way to disable this check. Even though we know it mirrors HTTP, from the HTTP protocol point of view, HTTPS is just some other, completely different, unknown protocol. It would be unsafe to follow the … Read more

FileNotFoundException while getting the InputStream object from HttpURLConnection

I don’t know about your Spring/JAXB combination, but the average REST webservice won’t return a response body on POST/PUT, just a response status. You’d like to determine it instead of the body. Replace InputStream response = con.getInputStream(); by int status = con.getResponseCode(); All available status codes and their meaning are available in the HTTP spec, … Read more

Sending files using POST with HttpURLConnection

I have no idea why the HttpURLConnection class does not provide any means to send files without having to compose the file wrapper manually. Here’s what I ended up doing, but if someone knows a better solution, please let me know. Input data: Bitmap bitmap = myView.getBitmap(); Static stuff: String attachmentName = “bitmap”; String attachmentFileName … Read more

Connecting to remote URL which requires authentication using Java

There’s a native and less intrusive alternative, which works only for your call. URL url = new URL(“location address”); URLConnection uc = url.openConnection(); String userpass = username + “:” + password; String basicAuth = “Basic ” + new String(Base64.getEncoder().encode(userpass.getBytes())); uc.setRequestProperty (“Authorization”, basicAuth); InputStream in = uc.getInputStream();

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)