标签:面试题 String url params new Android null conn GPS
-
发送请求的URL
-
@param params
-
请求参数,请求参数应该是name1=value1&name2=value2的形式。
-
@return URL所代表远程资源的响应
*/
public static String sendGet(String url, String params)
{
String result = “”;
BufferedReader in = null;
try
{
String urlName = url + “?” + params;
URL realUrl = new URL(urlName);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty(“accept”, “/”);
conn.setRequestProperty(“connection”, “Keep-Alive”);
conn.setRequestProperty(“user-agent”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)”);
// 建立实际的连接
conn.connect();
// 获取所有响应头字段
Map<String, List> map = conn.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet())
{
System.out.println(key + “—>” + map.get(key));
}
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
result += “\n” + line;
}
}
catch (Exception e)
{
System.out.println(“发送GET请求出现异常!” + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return result;
}
/**
-
向指定URL发送POST方法的请求
-
@param url
-
发送请求的URL
-
@param params
-
请求参数,请求参数应该是name1=value1&name2=value2的形式。
-
@return URL所代表远程资源的响应
*/
public static String sendPost(String url, String params)
{
PrintWriter out = null;
BufferedReader in = null;
String result = “”;
try
{
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty(“accept”, “/”);
conn.setRequestProperty(“connection”, “Keep-Alive”);
conn.setRequestProperty(“user-agent”,
“Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)”);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
result += “\n” + line;
}
}
catch (Exception e)
{
System.out.println(“发送POST请求出现异常!” + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return result;
}
}
AccessNetwork
新建一个类AccessNetwork实现Runnable接口,实现手机端的前端页面与后端逻辑处理能整合。
通过向服务端请求数据,并找到前端组件,将服务端响应的数据放到前端显示。
class AccessNetwork implements Runnable{
private String op ;
private String url;
private String params;
private Handler h;
public AccessNetwork(String op, String url, String params,Handler h) {
super();
this.op = op;
this.url = url;
this.params = params;
this.h = h;
}
@Override
public void run() {
JSONObject jsonObject = null;
String nowLongitude = null;
String nowLatitude = null;
String speedValue = “0”;
String Temperature = “0”;
String result = null;
Message m = new Message();
EditText speed = (EditText) findViewById(R.id.speedValue);
EditText wendu = (EditText) findViewById(R.id.wenduzhi);
EditText gps = (EditText) findViewById(R.id.gps);
m.what = 0x123;
while (true) {
if (op.equals(“GET”)) {
// Log.i(“iiiiiii”,“发送GET请求”);
m.obj = GetPostUtil.sendGet(url, params);
result = GetPostUtil.sendGet(url, params);
//Log.i(“iiiiiii”,">>>>>>>>>>>>"+m.obj);
}
if (op.equals(“POST”)) {
Log.i(“iiiiiii”, “发送POST请求”);
m.obj = GetPostUtil.sendPost(url, params);
result = GetPostUtil.sendGet(url, params);
Log.i(“gggggggg”, “>>>>>>>>>>>>” + m.obj);
}
try {
jsonObject = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (jsonObject != null) {
if (jsonObject.has(“Speed”))
speedValue = jsonObject.get(“Speed”).toString();
if (jsonObject.has(“Temperature”))
Temperature = jsonObject.get(“Temperature”).toString();
if (jsonObject.has(“longitude”))
nowLongitude = jsonObject.get(“longitude”).toString();
if (jsonObject.has(“latitude”))
nowLatitude = jsonObject.get(“latitude”).toString();
}
} catch(JSONException e){
e.printStackTrace();
}
if (carStatus == 1)
listenGps(nowLongitude, nowLatitude);
longitude = nowLongitude;
latitude = nowLatitude;
gps.setText(“经度” + longitude + " 纬度" + latitude);
speed.setText(speedValue);
wendu.setText(Temperature);
// h.sendMessage(m);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主类
主类新建副线程AccessNetwork并指定url请求。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button getdata;
ImageView carStatus_;
Thread get;
String longitude;
String latitude;
int carStatus=0;//carStatus=0为车未锁 1为车锁住了
@Override
protected void onCreate(Bundle savedInstanceSta
te) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getdata=(Button)findViewById(R.id.getdata);
carStatus_=(ImageView) findViewById(R.id.carStatus);
getdata.setOnClickListener(this);
carStatus_.setOnClickListener(this);
final TextView textView=(TextView)findViewById(R.id.endtitle);
final Handler h = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0x123){
textView.setText(msg.obj.toString());
}
}
};
get=new Thread(new AccessNetwork(“GET”, “http://123.207.58.192:8080/getCarInfo”, null, h));
}
}
运行截图
.getdata);
carStatus_=(ImageView) findViewById(R.id.carStatus);
getdata.setOnClickListener(this);
carStatus_.setOnClickListener(this);
final TextView textView=(TextView)findViewById(R.id.endtitle);
final Handler h = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0x123){
textView.setText(msg.obj.toString());
}
}
};
get=new Thread(new AccessNetwork(“GET”, “http://123.207.58.192:8080/getCarInfo”, null, h));
}
}
运行截图
标签:面试题,String,url,params,new,Android,null,conn,GPS 来源: https://blog.csdn.net/m0_66264655/article/details/122760043
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。