gpt4 book ai didi

android - 我无法从我的 Android 应用程序调用 servlet

转载 作者:太空狗 更新时间:2023-10-29 14:33:04 24 4
gpt4 key购买 nike

这是我的安卓应用程序:

package org.me.sampleandroidservlet;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView output=(TextView)findViewById(R.id.main_label);
String result="";
try
{
HttpClient http=new DefaultHttpClient();
HttpGet httpget = new HttpGet(
"http://localhost:8084/SampleWebUser/SampleUser?hello=hi");
HttpResponse response = http.execute(httpget);
result=sb.toString();
output.setText(result);
}
catch(Exception ex)
{
ex.printStackTrace();
}

导入java.io.IOException;导入 java.io.PrintWriter;导入 javax.servlet.ServletException;导入 javax.servlet.http.HttpServlet;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;

/**
*
* @author Bert Verhelst <verhelst_bert@hotmail.com>
*/
public class SampleUser 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 ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet servlet1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>processing...</h1>");
out.println("</body>");
out.println("</html>");
}

// <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 {
processRequest(request, response);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
+ "Transitional//EN\">\n"
+ "<html>\n"
+ "<head><title>Hello WWW</title></head>\n"
+ "<body>\n"
+ "<h1>message recieved from getmethod is\n"
+ request.getParameter("hello")+"\n"
+ "</h1></body></html>");

}

/**
* 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 {
processRequest(request, response);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
+ "Transitional//EN\">\n"
+ "<html>\n"
+ "<head><title>Hello WWW</title></head>\n"
+ "<body>\n"
+ "<h1>dopost...</h1>\n"
+ "</body></html>");
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

最佳答案

您的代码看起来不完整:

试试这个 fragment :

String serverResponse =""; // it will hold the response returned by the server
try
{
HttpClient http=new DefaultHttpClient();
HttpGet httpget = new HttpGet(
"http://10.0.2.2:8084/SampleWebUser/SampleUser?hello=hi");
HttpResponse response = http.execute(httpget);

serverResponse = EntityUtils.toString(resEntity); // Server response
/*
Do your other stuff here.
*/
}
catch(Exception ex)
{
ex.printStackTrace();
}

关于android - 我无法从我的 Android 应用程序调用 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4721281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com