private void getDataByHttp()
{
String strUrl = "http://192.168.1.113:8080/TestHttp/MyJsp.jsp";
strUrl += "?";
try
{
strUrl += "userName=" + URLEncoder.encode("测试", "UTF-8");
strUrl += "&userPassword=" + "123456";
System.out.println(strUrl);
} catch (UnsupportedEncodingException e1)
{
e1.printStackTrace();
}
String resultData= "";
URL url = null;
try
{
url = new URL(strUrl);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
if(url != null)
{
HttpURLConnection httpURLConnection = null;
try
{
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(3000);
if(httpURLConnection.getResponseCode() != 200)
{
Log.e("链接失败","返回不是200--" + httpURLConnection.getResponseCode());
Message msg = new Message();
msg.what = 2;
handler.sendMessage(msg);
return;
}
InputStream input = httpURLConnection.getInputStream();
InputStreamReader streamReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String inputLine = null;
while((inputLine = bufferedReader.readLine()) != null)
{
resultData += inputLine + "\r\n";
}
bufferedReader.close();
streamReader.close();
input.close();
httpURLConnection.disconnect();
<span style="white-space:pre"> </span>System.out.println(resultData);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
Android使用HttpURLConnection发送Get请求
最新推荐文章于 2024-05-09 17:47:45 发布