/*
*
*/
package com.ewallet.citygo.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import com.ewallet.citygo.config.AppConfig;
// TODO: Auto-generated Javadoc
/**
* The Class JsonUtils.
*/
public class JsonUtils {
/**
* The Constant URL.
*/
public static final String URL = AppConfig.PHOTOSERVERIP;
public static final String RETRIEVE_MESSAGE_DETAIL = JsonUtils.URL + "SmartCatchCityGoWsWeb/ws/message/RETRIEVE_MESSAGE_DETAIL";
//public static final String URL = "http://125.227.157.216:9168/";//JsonUtils周邊商家圖庫 2014/08/20 廢棄
public static final String QUERY_NEARBY_BRANCH_BY_CONDITION = "SmartCatchCityGoWsWeb/ws/user/QUERY_NEARBY_BRANCHS_BY_CONDITIONS_AND_PAGING";
// public static final String QUERY_DISTRICT_BRANCHES = "user/QUERY_DISTRICT_BRANCHES_BY_CONDITIONS_AUTO_SORT_AND_PAGING";
public static final String QUERY_MESSAGES_BY_TYPE_PAGING = "SmartCatchCityGoWsWeb/ws/message/QUERY_MESSAGES_BY_TYPE_PAGING";
// public static final String QUERY_SERVICE_NAME_BY_CATEGORY = "SmartCatchCityGoWsWeb/ws/service_category/get_category";
public static final String QUERY_SERVICE_NAME_BY_CATEGORY = "SmartCatchCityGoWsWeb/ws/category/get_category";
public static final String SUCCESS_MESSAGE = "SUCCESS";
public static final String NOT_FOUND = "NOT_FOUND";
/**
* The Constant QUERY_NEARBY_BRANCH.
*/
public static final String QUERY_NEARBY_BRANCH = "user/QUERY_NEARBY_BRANCHS_BY_CONDITIONS_AND_PAGING";
// public static final String QUERY_DISTRICT_BRANCHES = "user/QUERY_DISTRICT_BRANCHES_BY_CONDITIONS_AUTO_SORT_AND_PAGING";
/** The Constant QUERY_MESSAGES_BY_TYPE_PAGING. */
/**
* String convert to json object.
*
* @param value the value
* @return the JSON object
*/
public static JSONObject stringConvertToJsonObject(String value) {
JSONObject object = null;
try {
object = new JSONObject(value);
} catch (Exception e) {
// TODO: handle exception
}
return object;
}
/**
* Post.
*
* @param url the url
* @param json the json
* @return the string
*/
public static String POST(String url, String json) {
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
//Log.d("JsonUtils POST json.toString()", json.toString());
// ** Alternative way to convert Person object to JSON string usin
// Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json.toString(), HTTP.UTF_8);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the
// content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// httpPost.setHeader("user-agent", "Java/1.7.0_17");
// httpPost.setHeader("connection", "keep-alive");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
e.printStackTrace();
}
// 11. return result
Utils.myLog(result);
return result;
}
/**
* Convert input stream to string.
*
* @param inputStream the input stream
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
private static String convertInputStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
沒有留言:
張貼留言