最进两天闲的无聊,搭建了一个Android和Server端通信的小实验。
实验内容:把数据新浪微博上爬下来保存在数据库中
客户端:Android
服务器啊:nodejs
数据库:mongodb
驱动:mongoose
核心代码:Android
@Override
protected Boolean doInBackground(String... params) {
boolean isGetInfo = false;
String urlText = params[0];
getInfo_url = params[1];
post = new HttpPost(getInfo_url);
try {
URL url = new URL(urlText);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {//若当前连接成功
isGetInfo = true;
InputStream inStream = conn.getInputStream();//打开输入流
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
result = new String(outStream.toByteArray());//新建result变量用于获取服务器端传回的字符串
System.out.println("result = " + result);
inStream.close();//关闭数据输入流
}
outStream.close();//关闭数据输出流
conn.disconnect();//关闭远程连接
} catch (Exception e) {
e.printStackTrace();
}
try{
JSONObject json = new JSONObject(result);
List<NameValuePair> par = new ArrayList<NameValuePair>();
par.add(new BasicNameValuePair("uid", json.getString("id")));
par.add(new BasicNameValuePair("screenname", json.getString( "screen_name")));
par.add(new BasicNameValuePair("location", json.getString("location")));
par.add(new BasicNameValuePair("description", json.getString( "description")));
par.add(new BasicNameValuePair("url", json.getString("url")));
par.add(new BasicNameValuePair("profile", json.getString("profile_image_url")));
par.add(new BasicNameValuePair("sex", json.getString("gender")));
post.setEntity(new UrlEncodedFormEntity(par,HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);
if(response.getStatusLine().getStatusCode()==200){
System.out.println("Success");
}else{
System.out.println("Failure");
}
}catch(Exception e){
e.printStackTrace();
}
return isGetInfo;
}
WebStorm:schema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://127.0.0.1/xhl_information');
var User = mongoose.model('user',{
userID:String,
screenName:String,
location :String,
description:String,
url:String,
profile_image:String,
sex :String
}) ;
exports.User = User;
user_manager.js
var schema = require('../db_schema/schemas');
var User = require('../model/User.js');
exports.addUser = function(req,res){
var userId = req.body.uid;
console.log(userId);
var screenName = req.body.screenname;
console.log(screenName);
var location = req.body.location;
console.log(location);
var description = req.body.description;
console.log(description);
var url = req.body.url;
console.log(url);
var profile_image = req.body.profile ;
console.log(profile_image);
var sex = req.body.sex;
console.log(sex);
var user = new schema.User({
userID:userId,
screenName:screenName,
location :location,
description:description,
url:url,
profile_image:profile_image,
sex :sex
}) ;
user.save(function(err,saveUser){
if(err){
res.end();
} else{
console.log("User save successfully");
res.end();
}
})
}
app.js
app.post('/user_add',user_manager.addUser);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我调用的是新浪的显示用户信息的接口,运行程序之后,个人信息就保存在数据库中了。
注意:我在存入本地连接的时候使用的url是http://xxxxx;
如果你服务器请求提交的路劲是https://xxxx,那你要继承SSLSocketFactory,可以参考这个http://blog.youkuaiyun.com/binyao02123202/article/details/7697462。