0
回答
能够在网页请求到数据,在代码中请求不到
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我能够在网页上请求到数据,但是再代码中请求不到数据

这是服务端

package wy.servlet.study;


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;


import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletDemo extends HttpServlet {
private PrintWriter out;
OutputStream ous;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s=request.getParameter("id");
System.out.print(s);
log(s);

ous=response.getOutputStream();
if (s.equals("2")) {
response.setContentType("object");

String string="这是第一个请求这是第一个请求这是第一个请求这是第一个请求这是第一个请求这是第一个请求";
byte [] b=string.getBytes();
ous.write(b);
ous.flush();
ous.close();
}else {
String string="id错误";
byte [] bytes=string.getBytes();
ous.write(bytes);
ous.flush();
ous.close();
}

}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ous=response.getOutputStream();
response.setStatus(200, "请求成功");
String s=request.getParameter("id");
System.out.print(s);
log(s);
if (s.equals("2")) {
response.setContentType("object");
String string="这是第一个请求这是第一个请求这是第一个请求这是第一个请求这是第一个请求这是第一个请求这是第一个请求";
byte [] b=string.getBytes();
ous.write(b);
ous.flush();
ous.close();
}else {
response.setStatus(400, "请求失败");
String string="id错误";
byte [] bytes=string.getBytes();
ous.write(bytes);
ous.flush();
ous.close();;
}
}


}
这是代码

public class MainActivity extends AppCompatActivity { private Button btnget;  private HttpURLConnection httpURLConnection;  URL url;  InputStream inStrm;   @Override  protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  btnget = (Button) findViewById(R.id.btn_get);  try { url = new URL("http://192.168.0.183:8080/JavaWeb_Servlet_Study_20161028/ServletDemo");  } catch (MalformedURLException e) {
            e.printStackTrace();  } btnget.setOnClickListener(new View.OnClickListener() { @Override  public void onClick(View v) {
                getInfo();   }
        });  } /**  * 获取服务器的数据  */  private void getInfo() { new Thread(new Runnable() { @Override  public void run() { try { httpURLConnection = (HttpURLConnection) url.openConnection();  httpURLConnection.setDoOutput(true);  httpURLConnection.setDoInput(true);  httpURLConnection.setUseCaches(false);  httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");  httpURLConnection.setRequestMethod("POST");  httpURLConnection.connect();  OutputStream os = httpURLConnection.getOutputStream();  String param = new String();  param = "?id=2" + URLEncoder.encode("汉字", "utf-8");  os.write(param.getBytes());  os.flush();  os.close();  httpURLConnection.setConnectTimeout(3000);  inStrm = httpURLConnection.getInputStream();  String s = inStrm.toString();  Log.e("TAG", "----------------------" + s);  } catch (IOException e) {
                    e.printStackTrace();  }
            }
        });  }
}
请求地址如下

http://192.168.0.183:8080/JavaWeb_Servlet_Study_20161028/ServletDemo?id=2

<无标签>
举报
wywywywywy
发帖于1年前 0回/92阅
顶部