0
我一直在將我的android設備連接到pc的本地tomcat服務器時遇到問題。以下是Android代碼..如何將pc上的本地tomcat服務器連接到真正的android設備上?
public class Nxtstp extends Activity implements OnClickListener {
EditText txt;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nxtstp);
btn =(Button) findViewById(R.id.Ent);
btn.setOnClickListener(this);
};
class conn extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
public void run() {
try{
URL url = new URL("http://10.0.2.2:8084/Wiremserv/servlet1");
URLConnection connection = url.openConnection();
//inputString = URLEncoder.encode(inputString, "UTF-8");
String msg="Verify";
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(msg);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
final String returnString="Verfied";
final String ret = in.readLine();
if(returnString==ret){
Context cont = getApplicationContext();
String ms="Connection verified";
int dur=Toast.LENGTH_SHORT;
Toast tst = Toast.makeText(cont, ms, dur);
tst.show();
Intent i = new Intent("com.lnct.wirem3.MAINSERV");
startActivity(i); }
in.close();
}catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
return null;
}
和下面的NetBeans servlet代碼:
public class servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public servlet1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().println("Hurray !! This Servlet Works");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
File song = new File("E:/Music/digit.mp3");
URL url = song.toURI().toURL();
Player player=Manager.createPlayer(url);
try {
int length = request.getContentLength();
byte[] input = new byte[length];
ServletInputStream sin = request.getInputStream();
int c,count=0;
while ((c = sin.read(input, count, input.length-count)) != -1) {
count +=c;
}
sin.close();
String str = new String(input);
response.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
if("Verify".equals(str)){
String st="Verified";
writer.write(st);
writer.flush();
writer.close();
}
}
catch (IOException e) {
try{
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print(e.getMessage());
response.getWriter().close();
} catch (IOException ioe) {
}
}
} catch (NoPlayerException ex) {
Logger.getLogger(servlet1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
現在什麼,我想的是,我想我的應用程序連接到這個servlet所以點擊驗證按鈕後,我的新活動就會啓動。該servlet只是比較字符串以發送驗證消息給android設備。也請告訴我的方式使我的Android設備(三星Galaxy S提前GT-I9070)