Para enviar no WS
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
JSONArray jArray = new JSONArray();
try {
jArray = userDao.carregarLogins();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray gSon = JSONArray.fromObject(jArray);
response.setContentType("application/json");
PrintWriter out = response.getWriter();
// Pega a Stream de Saída do servidor que sera utilizada para enviar
a
// resposta JSON
out.print(gSon);// Escreve a resposta no formato JSON na Stream de
saída
// que será recebida pela aplicação cliente
out.flush();
}
No android
private void JSONFile(String url){
try{
//Le a pagina
GetHttp http = new GetHttp(url);
// page contem a string json
page = http.page;
//nativo do SDK da o parse da string json
JSONObject object = (JSONObject) new
JSONTokener(page).nextValue();
// monta o array do retorno do json, lembrando que retorno
é o PAI do json impresso em page.
//{"retorno":[ {"campo1":"valor1"},{"campo2":"valor2"},
{"campo3":"valor3"} ]}
JSONArray message = object.getJSONArray("gSon");
//Percorre o array retornado
//campo1, 2 e 3 são os nós da string json
{"campo1":"valor1"},{"campo2":"valor2"},{"campo3":"valor3"}
for(int i=0;i<message.length();i++){
JSONObject lines = (JSONObject) new
JSONTokener(message.getString(i)).nextValue();
Log.d("WS", "[Retorno do WS]");
Log.d("WS", "Retorno: "+ lines.getString("usuario") );
Log.d("WS", "[/Retorno do WS]");
}
Log.d("WS", "Sincronizado com sucesso");
pd.dismiss();
}catch (Exception e) {
Log.e("WS",e.toString());
}
}
erro nessa linha
JSONArray message = object.getJSONArray("gSon");
02-13 01:49:43.931: ERROR/WS(938): java.lang.ClassCastException:
org.json.JSONArray
On 12 fev, 22:36, Adalto Junior <herdeiro.jun...@gmail.com> wrote:
> Posta o erro que fica melhor pra ajudar.
>
> Em 12/02/2012, às 21:31, Gustavo Costa escreveu:
>
>
>
>
>
>
>
>
>
> > protected void doPost(HttpServletRequest request,
> > HttpServletResponse response) throws ServletException, IOException {
>
> > JSONArray jArray = new JSONArray();
>
> > try {
> > jArray = userDao.carregarLogins();
> > } catch (Exception e) {
> > // TODO Auto-generated catch block
> > e.printStackTrace();
> > }
>
> > JSONObject json = JSONObject.fromObject(jArray);
>
> > response.setContentType("application/json");
>
> > PrintWriter out = response.getWriter();
> > // Pega a Stream de Saída do servidor que sera utilizada para enviar
> > a
> > // resposta JSON
> > out.print(json);// Escreve a resposta no formato JSON na Stream de
> > saída
> > // que será recebida pela aplicação cliente
> > out.flush();
> > }
>
> > public JSONArray carregarLogins() throws Exception {
> > Statement stm = null;
> > ResultSet rst = null;
> > StringBuffer sql = null;
>
> > abrirConexao();
>
> > JSONArray aUser = new JSONArray();
>
> > stm = Conexao.createStatement();
>
> > sql = new StringBuffer();
> > sql.append("SELECT * FROM USUARIO");
>
> > rst = stm.executeQuery(sql.toString());
>
> > while(rst.next()) {
> > UserVO oUser = new UserVO();
>
> > oUser.setIdUsuario(rst.getInt("id"));
> > oUser.setUsuario(rst.getString("login"));
>
> > aUser.add(oUser);
>
> > }
>
> > stm.close();
> > Conexao.close();
>
> > return aUser;
> > }
>
> > Da erro na linha
> > JSONObject json = JSONObject.fromObject(jArray);
>
> > Aguém sabe a solução ?
>
> > On 11 fev, 18:17, Marcelo Henrique <marceloh...@gmail.com> wrote:
> >>http://www.portalandroid.org/comunidade/viewtopic.php?f=7&t=11162
>
> >> Em 11 de fevereiro de 2012 18:13, Gustavo Costa
> >> <gustavodaco...@gmail.com>escreveu:
>
> >>> Galera estou fazendo um webservice e preciso pegar de um banco os
> >>> idUsuario e o Usuario para preecher uma sppiner em android.
>
> >>> Fiz um array no webservice e enviei para o android da seguinte forma.
> >>> JSONObject json = JSONObject.fromObject(aUser);
>
> >>> Agora como eu pego isso no Android!!
>
> >>> Valeu
>
> >>> Gustavo Costa
>
> >> --
> >> - Marcelo Henrique -
> >> "Se não puder se destacar pelo talento, vença pelo esforço." (Dave
> >> Weinbaum)






0 comentários:
Postar um comentário