Pessoal,
Estou tentando utilizar o método post para consumir um Web Service:
@Override
protected Integracao doInBackground(Void... params) {
Integracao retInt = null;
Gson gson = new Gson();
String urlCnx = urlWs+"integracao";
StringBuilder execucao = new StringBuilder();
HttpURLConnection urlConnection = null;
System.out.println("Preparando conexão WS - POST");
try {
URL url = new URL(urlCnx);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
out, "UTF-8"));
writer.write(json.toString());
writer.flush();
int code = urlConnection.getResponseCode();
if (code != 201) {
throw new IOException("Invalid response from server: " + code);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Log.i("data", line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
Log.d("Integração ",retInt.toString());
return retInt;
}
Porém está retornando o erro abaixo:
W/System.err: java.net.ProtocolException: Unexpected status line:
W/System.err: at com.android.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
W/System.err: at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:395)
W/System.err: at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:144)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:887)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:759)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:498)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:434)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:565)
W/System.err: at com.br.retcam.webservice.HttpServiceIntegracao.doInBackground(HttpServiceIntegracao.java:71)
W/System.err: at com.br.retcam.webservice.HttpServiceIntegracao.doInBackground(HttpServiceIntegracao.java:35)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err: at java.lang.Thread.run(Thread.java:764)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
Process: com.br.retcam, PID: 22110
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String com.br.retcam.entity.Integracao.toString()’ on a null object reference
at com.br.retcam.webservice.HttpServiceIntegracao.doInBackground(HttpServiceIntegracao.java:90)
at com.br.retcam.webservice.HttpServiceIntegracao.doInBackground(HttpServiceIntegracao.java:35)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Alguém tem alguma ideia do que pode ser?
Obrigado