First of all, I have been searching for an answer and none works actually, API is ok, url is ok.
I have a method builder which contructs an url and an API name, it was working fine, but one of the API sould attach the token in the url and the params via post, I have the trouble there
Post method I'm using
public Heap post(String... params) throws ApiException {
Heap answer = new Heap();
StringBuilder listedParams = new StringBuilder();
for (int i = 1; i < params.length - 1; i++) {
listedParams.append(params[i]);
}
try {
answer = new Heap(new JSONObject(new AsyncRequest().execute(urlBuildier(params[0]), listedParams.toString(), "post").get()));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return answer;
}
urlBuilder method I'm using
private String urlBuildier(String... parts) {
ArrayList<String> params = new ArrayList<>();
if (assets)
params.add(urlAssets);
else
params.add(url);
final StringBuilder helper = new StringBuilder();
Collections.addAll(params, parts);
Heap built = new Heap(params);
built.forEach(new Part() {
@Override
public void Run(Heap part) {
helper.append(part.get());
}
});
return helper.toString();
}
the async class
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, params[1]);
Request request = new Request.Builder()
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.url(params[0])
.build();
Response response = client.newCall(request).execute();
String responseStrng = response.body().string();
return responseStrng;
The problem ocurrs when the url needs to attach a token, example: https://url/api/apiname? This works with my post method
But: https://url/api/apiname?token=abc123 isn't working with my post method.
PDT: the unic thing I changed from the previous methods to work was my post and post async this way:
previus post method
public Heap post(String... params) throws ApiException {
Heap answer = new Heap();
try {
answer = new Heap(new JSONObject(new AsyncRequest().execute(urlBuildier(params), "post").get()));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return answer;
}
Previous okhttp async
final String[] post = params[0].split("[?]");
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, post[1]);
Request request = new Request.Builder()
.url(post[0])
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.build();
Response response = client.newCall(request).execute();
String responseStrng = response.body().string();
return responseStrng;
This is my log:
06-02 22:32:04.443 6335-6335/co.cardseed.cardseed W/System.err: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:160)
at org.json.JSONObject.<init>(JSONObject.java:173)
06-02 22:32:04.444 6335-6335/co.cardseed.cardseed W/System.err: at co.cardseed.cardseed.app.api.Api.post(Api.java:103)
at co.cardseed.cardseed.activities.WebActivity.onCreate(WebActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
I would apreciate any help.
Well, servermaster allow the send of the token via header ant it kinda solves the problem, but in android it never works. Thanks for all.