我在Android中做一个简单的应用程序。 Android应用程序有一个图像按钮。当我从Android客户端单击按钮时,请求转到servlet。现在我在从servlet到Android客户端获取字符串值并在ListView中设置这些值时遇到问题。
如何从servlet发送字符串数据并在Android客户端中接收数据,如何在ListView中设置此数据?
package com.activity;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class StateInfoActivity extends Activity{
ListView lst;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.state);
lst=(ListView) findViewById(R.id.lst);
txt=(TextView) findViewById(R.id.txt);
try{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://192.168.1.1:8084/TestApp/StateInfo");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
ArrayList<String> list=new ArrayList<String>();
String str="";
while(true){
int ch = in.read();
if(ch==-1) break;
str+=(char)ch;
}
//txt.set
//Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Ansh
*/
public class StateInfo extends HttpServlet{
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ArrayList<String> lst=new ArrayList<String>();
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ansh","tourism","tourism");
Statement st=con.createStatement();
String q="select state_nm from state_detail";
ResultSet rs=st.executeQuery(q);
while(rs.next()){
out.println(rs.getString(1));
}
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(StateInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(StateInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:0)
使用肥皂是最好的解决方案 在Android上你可以使用ksoap2