HttpUtil--Json发送

public class HttpUtil {

/**
* 检查网络连接是否可用
* @param context
* @return
*/
public static boolean checkNetworking(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nwi = cm.getActiveNetworkInfo();
if(nwi!=null){
return nwi.isAvailable();
}
return false;
}


/**
* 登陆
* @param jsonInfo
* @return
*/
public static boolean login(final Context context,JSONObject jsonInfo){
HttpPost request = new HttpPost(Constants.Login_URL); 
try {
// 绑定到请求 Entry 
StringEntity entity = new StringEntity(jsonInfo.toString());
request.setEntity(entity); 
request.setHeader("Content-Type", "application/json");
HttpClient httpClient = new DefaultHttpClient();
HttpParams params=httpClient.getParams(); //计算网络超时用. 
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 发送请求 
HttpResponse response = httpClient.execute(request); 

// 得到应答的字符串,这也是一个 JSON 数据  
String result = EntityUtils.toString(response.getEntity());  
System.out.println("login-result-->"+result);
if (response.getStatusLine().getStatusCode() == 200) {//登陆成功
System.out.println("StatusCode--login->"+response.getStatusLine().getStatusCode());
SharedPreferenceUtil util = new SharedPreferenceUtil(context);
util.setLoginData(result);
return true;
}else{
System.out.println("StatusCode--login->"+response.getStatusLine().getStatusCode());
return false;
}
} catch (final Exception e) {
 Looper.prepare();
 TipUtil.tip(context, "连接网络超时...请检查网络!");
 Looper.loop(); 
}
return false;
}


/**

* @param jsonInfo
* @return
*/
public static boolean logout(JSONObject loginObj){
HttpPost request = new HttpPost(Constants.Logout_URL); 
try {
// 绑定到请求 Entry 
StringEntity entity = new StringEntity(loginObj.toString());
request.setEntity(entity); 
request.setHeader("Content-Type", "application/json");
// 发送请求 
HttpResponse response = new DefaultHttpClient().execute(request); 
// 得到应答的字符串,这也是一个 JSON 数据  
String result = EntityUtils.toString(response.getEntity());  
System.out.println("logout-result-->"+result);
if (response.getStatusLine().getStatusCode() == 200) {//注销成功
return true;
}else{
System.out.println("StatusCode--logout->"+response.getStatusLine().getStatusCode());
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}









/**
* 下载频道数据
* @return
*/
public static String downloadChannelData(){
try {
URL uri = new URL(Constants.Channels_URL) ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadChannelData-->"+source.toString());
return source.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


/**
* 下载program数据
* @return
*/
public static String downloadProgramsData() {
try {
URL uri = new URL(Constants.Program_URL) ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadProgramsData-->"+source.toString());
return source.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 下载program数据
* @return
*/
public static String downloadTopicsData() {
try {
URL uri = new URL(Constants.Topic_URL) ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadTopicsData-->"+source.toString());

return source.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}







/**
* {"key":"虎"}
* @param string
*/
public static String searchByCondition(String key,String searchCondition) {
JSONObject searchInfo = new JSONObject();
HttpPost request = new HttpPost(searchCondition); 
try {
searchInfo.put("key", key);
StringEntity entity = new StringEntity(searchInfo.toString());
request.setEntity(entity); 

request.setHeader("Content-Type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(request);

String searchResult = EntityUtils.toString(response.getEntity());  
System.out.println("searchResult-->"+searchResult);

if (response.getStatusLine().getStatusCode() == 200) {//登陆成功
System.out.println("SearchStatusCode--->"+response.getStatusLine().getStatusCode());
return searchResult;
}else{
System.out.println("SearchStatusCode--->"+response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}



public static List<Topic> downloadProgramTopic(int programId) {
try {
URL uri = new URL(Constants.Program_URL+"/"+programId+"/topics") ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadProgramTopic--id>"+source.toString());
return JsonUtil.topicDataHandle(source.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* http://tvsrv.webhop.net:8080/api/programs/???/posts
* @param ???-->programId
* @return
*/
public static String downloadProgramPosts(int programId) {
try {
URL uri = new URL(Constants.Program_URL+"/"+programId+"/posts") ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadProgramPost--programId>"+source.toString());
return source.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


/**
* http://tvsrv.webhop.net:8080/api/topics/???/posts
* @param ???-->topicId
* @return
*/
public static String downloadTopicPosts(int topicId){
try {
URL uri = new URL(Constants.Topic_URL+"/"+topicId+"/posts") ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadTopicPosts--topicId>"+source.toString());
return source.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}



/**
* 添加关注
* http://tvsrv.webhop.net:8080/api/follow
* {"id":"6","followingid":"1"}
* @param userId
* @param followId
*/
public static boolean addFollow(int userId, int followId) {
JSONObject unFollowInfo = new JSONObject();
HttpPost request = new HttpPost(Constants.AddFollow);
try {
unFollowInfo.put("id", userId);
unFollowInfo.put("followingid", followId);
request.setEntity(new StringEntity(unFollowInfo.toString()));
request.setHeader("Content-Type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(request);
String result = EntityUtils.toString(response.getEntity());  
System.out.println("addFollow-result-->"+result);
if (response.getStatusLine().getStatusCode() == 200) {//follow成功
return true;
}else{
System.out.println("StatusCode--addFollow->"+response.getStatusLine().getStatusCode());
return false;
}
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

/**
* 取消关注
* http://tvsrv.webhop.net:8080/api/unfollow
* {"id":"6","followingid":"1"}
* @param userId
* @param followId
*/
public static boolean unFollow(int userId, int followId) {
JSONObject unFollowInfo = new JSONObject();
HttpPost request = new HttpPost(Constants.UnFollow);
try {
unFollowInfo.put("id", userId);
unFollowInfo.put("followingid", followId);
request.setEntity(new StringEntity(unFollowInfo.toString()));
request.setHeader("Content-Type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(request);
String result = EntityUtils.toString(response.getEntity());  
System.out.println("unFollow-result-->"+result);
if (response.getStatusLine().getStatusCode() == 200) {//unFollow成功
return true;
}else{
System.out.println("StatusCode--unFollow->"+response.getStatusLine().getStatusCode());
return false;
}


} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}




/**
* http://tvsrv.webhop.net:8080/api/users/2/mycomments
* @param type 
* @param userId
* @return
*/
public static List<?> downloadUserInfo(String type, int userId) {
URL uri;
try {
uri = new URL(Constants.User_URL+"/"+userId+"/"+type);

HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadUserInfo->"+type+"<-userId->"+userId+":"+source.toString());
if(type.equals("mycomments")||type.equals("commentmys")){
return JsonUtil.commentsDataHandle(source.toString());
}else if(type.equals("followers")){
List<User> users = new ArrayList<User>();
JSONArray jsonArray = new JSONArray(source.toString());
for(int i = 0;i<jsonArray.length();i++){
JSONObject userObj = jsonArray.getJSONObject(i);
User user = JsonUtil.handleJSONChild_User(userObj);
users.add(user);
}
return users;
}else if(type.equals("followings")){
List<User> users = new ArrayList<User>();
JSONArray jsonArray = new JSONArray(source.toString());
for(int i = 0;i<jsonArray.length();i++){
JSONObject userObj = jsonArray.getJSONObject(i);
User user = JsonUtil.handleJSONChild_User(userObj);
users.add(user);
}
return users;
}else if(type.equals("favorite-programs")){
return JsonUtil.programDataHandle(source.toString());
}else if(type.equals("favorite-topics")){
return JsonUtil.topicDataHandle(source.toString());
}else if(type.equals("favorite-posts")){
return JsonUtil.postDataHandle(source.toString());
}else if(type.equals("myreposts")){
return JsonUtil.repostsDataHandle(source.toString());
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}








/**
* 添加收藏
* http://tvsrv.webhop.net:8080/api/follow
* {"id":"2"}
* @param userId
* @param followId
*/
public static boolean addFavorite(int userId, int followId,String type) {
JSONObject unFollowInfo = new JSONObject();
HttpPost request = new HttpPost(Constants.User_URL+"/"+userId+"/favorite-"+type);
try {
unFollowInfo.put("id", followId);
request.setEntity(new StringEntity(unFollowInfo.toString()));
request.setHeader("Content-Type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(request);
String result = EntityUtils.toString(response.getEntity());  
System.out.println("addFavorite-"+type+"-result-->"+result);
if (response.getStatusLine().getStatusCode() == 200) {
return true;
}else{
System.out.println("addFavorite-"+type+"StatusCode--->"+response.getStatusLine().getStatusCode());
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 取消收藏
* http://tvsrv.webhop.net:8080/api/users/5/unfavorite-programs
* {"id":"1"}
* @param userId
* @param type 
* @param followId
*/
public static boolean unFavorite(int userId, int typeId, String type) {
JSONObject unFollowInfo = new JSONObject();
HttpPost request = new HttpPost(Constants.User_URL+"/"+userId+"/unfavorite-"+type);
try {
unFollowInfo.put("id", typeId);
request.setEntity(new StringEntity(unFollowInfo.toString()));
request.setHeader("Content-Type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(request);
String result = EntityUtils.toString(response.getEntity());  
System.out.println("unFavorite-result-->"+result);
if (response.getStatusLine().getStatusCode() == 200) {//unFavorite成功
return true;
}else{
System.out.println("StatusCode--unFavorite->"+response.getStatusLine().getStatusCode());
return false;
}

} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}




public static User findUserById(int userId) {
try {
URL uri = new URL(Constants.User_URL+"/"+userId) ;
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("findUserById--userId>"+source.toString());
return JsonUtil.handleJSONChild_User(new JSONObject(source.toString()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}




public static List<Repost> downloadReposts(int postId) {
URL uri;
try {
uri = new URL(Constants.Post_URL+"/"+postId+"/reposts");

HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadReposts--postId->"+postId+":"+source.toString());

return JsonUtil.repostsDataHandle(source.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}




public static List<?> downloadHots(String what, String type) {

URL uri;
try {
uri = new URL(Constants.Hot_URL+"/"+what+"/"+type);

HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
Source source = new Source(conn);
System.out.println("downloadHots--type->"+what+type+":"+source.toString());
if(what.equals("programs")){
return JsonUtil.programDataHandle(source.toString());
}else if(what.equals("topics")){
return JsonUtil.topicDataHandle(source.toString());
}else if(what.equals("users")){
return JsonUtil.userDataHandle(source.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}




/**
* {"programid":"2","name":"gone,gone,gone","createby":"2"}
* @param programId
* @param userId
* @param name
* @param context
* @return
*/
public static boolean addTopic(int programId, int userId, String name,Context context) {
HttpPost request = new HttpPost(Constants.Topic_URL); 
try {
JSONObject jsonInfo = new JSONObject();
jsonInfo.put("programId", programId);
jsonInfo.put("name", name);
jsonInfo.put("createby", userId);
// 绑定到请求 Entry 
StringEntity entity = new StringEntity(jsonInfo.toString());
request.setEntity(entity); 
request.setHeader("Content-Type", "application/json");


HttpClient httpClient = new DefaultHttpClient();
HttpParams params=httpClient.getParams(); //计算网络超时用. 
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 发送请求 
HttpResponse response = httpClient.execute(request); 

if (response.getStatusLine().getStatusCode() == 200) {//登陆成功
System.out.println("StatusCode--addTopic->"+response.getStatusLine().getStatusCode());
return true;
}else{
System.out.println("StatusCode--addTopic->"+response.getStatusLine().getStatusCode());
return false;
}
} catch (final Exception e) {
 Looper.prepare();
 TipUtil.tip(context, "连接网络超时...请检查网络!");
 Looper.loop(); 
}
return false;
}




/**
* {"c":"this is a test post message","u":"1","p":"1", "t": "1"}
*   http://tvsrv.webhop.net:8080/api/posts
* {"c":"repost message","u":"3","p":"1", "t": "1"}
*   http://tvsrv.webhop.net:8080/api/posts/1/reposts
* @param type  -->post / repost /comment
* @param c content
* @param c2 
* @param u userId
* @param p programId / postId /postId
* @param t topicId
* @param context
* @return
*/
public static boolean addPost(String type, String c, int u, int p, int t,Context context) {
HttpPost request = null;
if(type.equals("post")){
request = new HttpPost(Constants.Post_URL); 
}else if(type.equals("repost")){
request = new HttpPost(Constants.Post_URL+"/"+p+"/reposts");
}else if(type.equals("comment")){
request = new HttpPost(Constants.Post_URL+"/"+p+"/comments");
}
try {
JSONObject jsonInfo = new JSONObject();
jsonInfo.put("c", c);
jsonInfo.put("u", u);
jsonInfo.put("p", p);
jsonInfo.put("t", t);
// 绑定到请求 Entry 
StringEntity entity = new StringEntity(jsonInfo.toString());
request.setEntity(entity); 
request.setHeader("Content-Type", "application/json");

HttpClient httpClient = new DefaultHttpClient();
HttpParams params=httpClient.getParams(); //计算网络超时用. 
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 发送请求 
HttpResponse response = httpClient.execute(request); 

if (response.getStatusLine().getStatusCode() == 200) {//登陆成功
System.out.println("StatusCode--"+type+"->"+response.getStatusLine().getStatusCode());
return true;
}else{
System.out.println("StatusCode--"+type+"->"+response.getStatusLine().getStatusCode());
TipUtil.tip(context,"发布失败!StatusCode-"+type+"->"+response.getStatusLine().getStatusCode());
return false;
}
} catch (final Exception e) {
 TipUtil.tip(context, "连接网络超时...请检查网络!");
}
return false;
}




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值