Sometimes when you try doing a background progress or performing network request. Android will give this error. So what you need to do is passing the execution to AsyncTask class to seperate it from the main thread. This can refer as example down below :
private class sendFile extends AsyncTask<String, Void, String>
{
private Exception exception;
@Override protected String doInBackground(String... urls) {
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"apikey\"\r\n\r\n4clawHkacJICpzNv_1lTC8IGGBxyVADY4G3rpeMRWKg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request requestQueue = new Request.Builder()
.url("https://api.webempath.net/v2/analyzeWav")
.post(body)
.addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "1ac1340c-beb9-162d-f583-e2bd77b7355a")
.build();
try {
Response response = client.newCall(requestQueue).execute();
String mResponse = response.body().string();
Log.i("Response",mResponse);
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e) {
this.exception = e;
return null;
}
return null;
}
@Override protected void onPostExecute(String result) {
responseText.setText(result);
}
}
No comments:
Post a Comment