URLConnection 和 HTTPClient 的比较,记一次字节跳动 Android 社招面试
//response = br.readLine();?
response = response + readLine;?
}?
is.close();?
br.close();?
uRLConnection.disconnect();?
return?response;?
}?catch?(MalformedURLException e) {?
e.printStackTrace();?
returnnull;?
}?catch?(IOException e) {?
e.printStackTrace();?
returnnull;?
}?
}
HTTPClient
String urlAddress = "http://192.168.1.102:8080/qualityserver/login.do";?
public?HttpClientServer(){?
}?
public?String doGet(String username,String password){?
String getUrl = urlAddress + "?username="+username+"&password="+password;?
HttpGet httpGet =?new?HttpGet(getUrl);?
HttpParams hp = httpGet.getParams();?
hp.getParameter("true");?
//hp.?//httpGet.setp?
HttpClient hc =?new?DefaultHttpClient();?
try?{?
HttpResponse ht = hc.execute(httpGet);?
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){?
HttpEntity he = ht.getEntity();?
InputStream is = he.getContent();?
BufferedReader br =?new?BufferedReader(new?InputStreamReader(is));?
String response = "";?
String readLine =?null;?
while((readLine =br.readLine()) !=?null){?
//response = br.readLine();?
response = response + readLine;?
}?
is.close();?
br.close();?
//String str = EntityUtils.toString(he);?
System.out.println("========="+response);?
return?response;?
}else{?
return?"error";?
}?
}?catch?(ClientProtocolException e) {?
//?TODO Auto-generated catch block?
e.printStackTrace();?
return?"exception";?
}?catch?(IOException e) {?
//?TODO Auto-generated catch block?
e.printStackTrace();?
return?"exception";?
}?
}?
public?String doPost(String username,String password){?
//String getUrl = urlAddress + "?username="+username+"&password="+password;?
HttpPost httpPost =?new?HttpPost(urlAddress);?
List params =?new?ArrayList();?
NameValuePair pair1 =?new?BasicNameValuePair("username", username);?
NameValuePair pair2 =?new?BasicNameValuePair("password", password);?
params.add(pair1);?
params.add(pair2);?
HttpEntity he;?
try?{?
he =?new?UrlEncodedFormEntity(params, "gbk");?
httpPost.setEntity(he);?
}?catch?(UnsupportedEncodingException e1) {?
//?TODO Auto-generated catch block?
e1.printStackTrace();?
}?
HttpClient hc =?new?DefaultHttpClient();?
try?{?
HttpResponse ht = hc.execute(httpPost);?
//连接成功?
if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){?
HttpEntity het = ht.getEntity();?
InputStream is = het.getContent();?
BufferedReader br =?new?BufferedReader(new?InputStreamReader(is));?
String response = "";?
String readLine =?null;?
while((readLine =br.readLine()) !=?null){?
//response = br.readLine();?
response = response + readLine;?
}?
is.close();?
br.close();?
//String str = EntityUtils.toString(he);?
System.out.println("=========&&"+response);?
return?response;?
}else{?
return?"error";?
}?
}?catch?(ClientProtocolException e) {?
//?TODO Auto-generated catch block?
e.printStackTrace();?
return?"exception";?
}?catch?(IOException e) {?
//?TODO Auto-generated catch block?
e.printStackTrace();?
return?"exception";?
}?
}
servlet 端 json 转化:?
resp.setContentType("text/json");?
resp.setCharacterEncoding("UTF-8");?
toDo =?new?ToDo();?
List<UserBean> list =?new?ArrayList<UserBean>();?
list = toDo.queryUsers(mySession);?
String body;?
//设定 JSON?
JSONArray array =?new?JSONArray();?
for(UserBean bean : list)?
{?
JSONObject obj =?new?JSONObject();?
try
{?
obj.put("username", bean.getUserName());?
obj.put("password", bean.getPassWord());?
}catch(Exception e){}?
array.add(obj);?
}?
pw.write(array.toString());?
System.out.println(array.toString());
android 端接收:
String urlAddress = "http://192.168.1.102:8080/qualityserver/result.do";?
String body =?
getContent(urlAddress);?
JSONArray array =?new?JSONArray(body);?
for(int?i=0;i<array.length();i++)?
{?
obj = array.getJSONObject(i);?
sb.append("用户名:").append(obj.getString("username")).append("\t");?
sb.append("密码:").append(obj.getString("password")).append("\n");?
HashMap<String, Object> map =?new?HashMap<String, Object>();?
try?{?
userName = obj.getString("username");?
passWord = obj.getString("password");?
}?catch?(JSONException e) {?
e.printStackTrace();?
}?
map.put("username", userName);?
map.put("password", passWord);?
listItem.add(map);?
}?
}?catch?(Exception e) {?
//?TODO Auto-generated catch block?
e.printStackTrace();?
}?
if(sb!=null)?
{?
showResult.setText("用户名和密码信息:");?
showResult.setTextSize(20);?
}?else
extracted();?
//设置 adapter?
SimpleAdapter simple =?new?SimpleAdapter(this,listItem,?
android.R.layout.simple_list_item_2,?
new?String[]{"username","password"},?
newint[]{android.R.id.text1,android.R.id.text2});?
listResult.setAdapter(simple);?
listResult.setOnItemClickListener(new?OnItemClickListener() {?
@Override?
publicvoid?onItemClick(AdapterView<?> parent, View view,?
int?position,?long?id) {?
int?positionId = (int) (id+1);?
Toast.makeText(MainActivity.this, "ID:"+positionId, Toast.LENGTH_LONG).show();?
}?
});?
}?
privatevoid?extracted() {?
showResult.setText("没有有效的数据!");?
}?
//和服务器连接?
private?String getContent(String url)throws?Exception{?
StringBuilder sb =?new?StringBuilder();?
HttpClient client =new?DefaultHttpClient();?
HttpParams httpParams =client.getParams();?
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);?
HttpConnectionParams.setSoTimeout(httpParams, 5000);?
HttpResponse response = client.execute(new?HttpGet(url));?
HttpEntity entity =response.getEntity();?
if(entity !=null){?
BufferedReader reader =?new?BufferedReader(new?InputStreamReader?
(entity.getContent(),"UTF-8"),8192);?
String line =null;?
while?((line= reader.readLine())!=null){?
sb.append(line +"\n");?
}?
reader.close();?
}?
return?sb.toString();?
}
URLConnection
HTTPClient
Proxies and SOCKS
Authorization
Methods
Headers
Automatic Redirection Handling
Persistent Connections
Pipelining of Requests
Can handle protocols other than HTTP
Can do HTTP over SSL (https)
Source code available
|
Full support in Netscape browser, appletviewer, and applications (SOCKS: Version 4 only); no additional limitations from security policies.
|
Full support (SOCKS: Version 4 and 5); limited in applets however by security policies; in Netscape can't pick up the settings from the browser.
|
|
Full support for Basic Authorization in Netscape (can use info given by the user for normal accesses outside of the applet); no support in appletviewer or applications.
|
Full support everywhere; however cannot access previously given info from Netscape, thereby possibly requesting the user to enter info (s)he has already given for a previous access. Also, you can add/implement additional authentication mechanisms yourself.
|
|
Only has GET and POST.
|
Has HEAD, GET, POST, PUT, DELETE, TRACE and OPTIONS, plus any arbitrary method.
|
|
Currently you can only set any request headers if you are doing a POST under Netscape; for GETs and the JDK you can't set any headers.?
Under Netscape 3.0 you can read headers only if the resource was returned with a Content-length header; if no Content-length header was returned, or under previous versions of Netscape, or using the JDK no headers can be read.
|
Allows any arbitrary headers to be sent and received.
|
|
Yes.
|
结语
网上高级工程师面试相关文章鱼龙混杂,要么一堆内容,要么内容质量太浅, 鉴于此我整理了上述安卓开发高级工程师面试题以及答案。希望帮助大家顺利进阶为高级工程师。目前我就职于某大厂安卓高级工程师职位,在当下大环境下也想为安卓工程师出一份力,通过我的技术经验整理了面试经常问的题,答案部分是一篇文章或者几篇文章,都是我认真看过并且觉得不错才整理出来。
大家知道高级工程师不会像刚入门那样被问的问题一句话两句话就能表述清楚,所以我通过过滤好文章来帮助大家理解。

现在都说互联网寒冬,其实只要自身技术能力够强,咱们就不怕!我这边专门针对 Android 开发工程师整理了一套【Android 进阶学习视频】、【全套 Android 面试秘籍】、【Android 知识点 PDF】。
**本文已被[CODING 开源项目:《Android 学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》](https:
《Android 学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
//docs.qq.com/doc/DSkNLaERkbnFoS0ZF)收录**
评论