Pessoal,
Estou recebendo o erro abaixo ao enviar os dados do aluno para o servidor.
05-23 13:59:24.640: E/AndroidRuntime(21667): FATAL EXCEPTION: AsyncTask #1
05-23 13:59:24.640: E/AndroidRuntime(21667): Process: com.tads.listaalunos, PID: 21667
05-23 13:59:24.640: E/AndroidRuntime(21667): java.lang.RuntimeException: An error occured while executing doInBackground()
05-23 13:59:24.640: E/AndroidRuntime(21667): at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-23 13:59:24.640: E/AndroidRuntime(21667): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.lang.Thread.run(Thread.java:818)
05-23 13:59:24.640: E/AndroidRuntime(21667): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.caelum.com.br/mobile
05-23 13:59:24.640: E/AndroidRuntime(21667): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:619)
05-23 13:59:24.640: E/AndroidRuntime(21667): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:307)
05-23 13:59:24.640: E/AndroidRuntime(21667): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:575)
05-23 13:59:24.640: E/AndroidRuntime(21667): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
05-23 13:59:24.640: E/AndroidRuntime(21667): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)
05-23 13:59:24.640: E/AndroidRuntime(21667): at com.tads.listaalunos.suport.WebClient.post(WebClient.java:31)
05-23 13:59:24.640: E/AndroidRuntime(21667): at com.tads.listaalunos.task.EnviaAlunosTask.doInBackground(EnviaAlunosTask.java:42)
05-23 13:59:24.640: E/AndroidRuntime(21667): at com.tads.listaalunos.task.EnviaAlunosTask.doInBackground(EnviaAlunosTask.java:1)
05-23 13:59:24.640: E/AndroidRuntime(21667): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-23 13:59:24.640: E/AndroidRuntime(21667): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-23 13:59:24.640: E/AndroidRuntime(21667): ... 4 more
05-23 13:59:25.268: E/WindowManager(21667): android.view.WindowLeaked: Activity com.tads.listaalunos.ListaAlunosActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1ef38d7 V.E..... R......D 0,0-684,322} that was originally added here
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:375)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-23 13:59:25.268: E/WindowManager(21667): at android.app.Dialog.show(Dialog.java:298)
05-23 13:59:25.268: E/WindowManager(21667): at android.app.ProgressDialog.show(ProgressDialog.java:116)
05-23 13:59:25.268: E/WindowManager(21667): at android.app.ProgressDialog.show(ProgressDialog.java:99)
05-23 13:59:25.268: E/WindowManager(21667): at android.app.ProgressDialog.show(ProgressDialog.java:94)
05-23 13:59:25.268: E/WindowManager(21667): at com.tads.listaalunos.task.EnviaAlunosTask.onPreExecute(EnviaAlunosTask.java:28)
05-23 13:59:25.268: E/WindowManager(21667): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
05-23 13:59:25.268: E/WindowManager(21667): at android.os.AsyncTask.execute(AsyncTask.java:535)
05-23 13:59:25.268: E/WindowManager(21667): at com.tads.listaalunos.ListaAlunosActivity.onOptionsItemSelected(ListaAlunosActivity.java:190)
05-23 13:59:25.268: E/WindowManager(21667): at android.app.Activity.onMenuItemSelected(Activity.java:2892)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:538)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:84)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
05-23 13:59:25.268: E/WindowManager(21667): at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:187)
05-23 13:59:25.268: E/WindowManager(21667): at android.widget.AdapterView.performItemClick(AdapterView.java:300)
05-23 13:59:25.268: E/WindowManager(21667): at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
05-23 13:59:25.268: E/WindowManager(21667): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
05-23 13:59:25.268: E/WindowManager(21667): at android.widget.AbsListView.onTouchUp(AbsListView.java:3845)
05-23 13:59:25.268: E/WindowManager(21667): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3617)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.View.dispatchTouchEvent(View.java:8393)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2426)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
05-23 13:59:25.268: E/WindowManager(21667): at android.widget.PopupWindow$PopupViewContainer.dispatchTouchEvent(PopupWindow.java:1682)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.View.dispatchPointerEvent(View.java:8583)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4091)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3957)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3562)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3528)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3638)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3536)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3695)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3562)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3528)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3536)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3509)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5806)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5745)
05-23 13:59:25.268: E/WindowManager(21667): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5716)
05-23 13:59:25.268: E/WindowManager(21667): at android
Meu codigo está conforme abaixo:
package com.tads.listaalunos.converter;
import java.util.List;
import org.json.JSONException;
import org.json.JSONStringer;
import com.tads.listaalunos.modelo.Aluno;
public class AlunoConverter {
/**
* Converte um array de Alunos para um string JSON com a estrutura abaixo:
*
* {"chave" : "valor"}
*
* {list :
* [
* { aluno :
* [
* {"nome" : "Aluno1", "nota" : "nota1"},
* {"nome" : "Aluno2", "nota" : "nota2"} *
* ]
* }
* ]
* }
*/
public String toJSON(List<Aluno> alunos) {
JSONStringer js = new JSONStringer();
try {
js.object().key("list").array();
js.object().key("aluno").array();
for(Aluno aluno : alunos) {
js.object();
js.key("nome").value(aluno.getNome());
js.key("nota").value(aluno.getNota());
js.endObject();
}
js.endArray().endObject();
js.endArray().endObject();
} catch (JSONException e) {
e.printStackTrace();
}
return js.toString();
}
}
package com.tads.listaalunos.suport;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
@SuppressWarnings("deprecation")
public class WebClient {
private String url;
public WebClient(String url) {
super();
this.url = url;
}
public String post(String json) {
HttpPost post = new HttpPost(url);
try {
post.setEntity(new StringEntity(json));
post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
return EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
package com.tads.listaalunos.task;
import java.util.List;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import com.tads.listaalunos.converter.AlunoConverter;
import com.tads.listaalunos.dao.AlunoDao;
import com.tads.listaalunos.modelo.Aluno;
import com.tads.listaalunos.suport.WebClient;
public class EnviaAlunosTask extends AsyncTask<Object, Object, String> {
private Context context;
private ProgressDialog progress;
public EnviaAlunosTask(Context context) {
super();
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(context, "Aguarde...", "Enviando Alunos para o servidor...");
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(Object... params) {
AlunoDao dao = new AlunoDao(context);
List<Aluno> alunosEnvio = dao.getLista();
String json = new AlunoConverter().toJSON(alunosEnvio);
String media = new WebClient("www.caelum.com.br/mobile").post(json);
return media;
}
@Override
protected void onPostExecute(String media) {
// TODO Auto-generated method stub
super.onPostExecute(media);
progress.dismiss();
Toast.makeText(context, "Media: "+media, Toast.LENGTH_SHORT).show();
}
}