Android Question SetContentType

marcel

Active Member
Licensed User
Longtime User
Hi,

I am trying to use the following code to set the content type:

B4X:
    Dim job As HttpJob
    job.Initialize("datadownload",Me)
    job.Download(URL)
    job.GetRequest.SetContentType("application/json")

but when I do this I get the following error:

B4X:
java.lang.RuntimeException: Only Post / Put requests support this method.
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.SetContentType(HttpClientWrapper.java:426)
    at b4a.example.dbupdate._service_start(dbupdate.java:255)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at b4a.example.dbupdate.handleStart(dbupdate.java:93)
    at b4a.example.dbupdate.onStartCommand(dbupdate.java:68)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2719)
    at android.app.ActivityThread.access$2100(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5139)
    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:796)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
    at dalvik.system.NativeStart.main(Native Method)

What is wrong with this code? I am using httputils2 (v2.01)
 

eurojam

Well-Known Member
Licensed User
Longtime User
Get requests should not have a content-type because they do not have request entity...in other words, you don't need it. In the job done sub you have to know what the stream is (bitmap, text so on) and decide what to do...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Usually not.
It depends in the requirements of the server
 
Upvote 0

marcel

Active Member
Licensed User
Longtime User
The server requires to set the content type and I can also post information. But I am not sure how to do this...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Here an example of my DropboxClass

B4X:
    Dim job As HttpJob
    job.Initialize("getaccount",Me)
    job.PostString(mApiV2url&"users/get_account",$"{"account_id":"${accountID}"}"$)
    job.GetRequest.SetHeader("Authorization", "Bearer "&mAccessToken)
    job.GetRequest.SetHeader("Content-Type", "application/json")
    job.GetRequest.SetContentType("application/json")
    job.GetRequest.SetContentEncoding("text/plain")

$"{"account_id":"${accountID}"}"$ is the content who will be POSTed.

For ex:
{"account_id":"1234"}
 
Upvote 0
Top