Android:将 Gson 库使用 HttpURLConnection API 创建的 JSON 字符串发送到本地主机
Android: Send JSON String created by Gson library with HttpURLConnection API to the localhost
我有 Gson 库创建的 JSON 字符串,我想用 HttpURLConnection API "GET" 将这个字符串发送到本地主机服务器 (wamp)。我将 运行 我的 S4 设备上的应用程序通过 USB 电缆连接到我的笔记本电脑。
感谢任何帮助。
我有这个 JSON 字符串:
{
"latitude":80.86907321,
"longitude":15.66542435,
"formatted":"22.04.2015 11:11:00",
"route":4
}
我里面有这个方法 class "MyLocationListener":
private String convertToJSON(double pLong, double pLat, String formatted) {
//envelop the data in JSON format.
Data d = new Data(pLat, pLong, formatted,route_number);
Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new DataSerializer()).create();
return gson.toJson(d);
}
首先,你要了解AsyncTask。
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
这里link给AndroidDevelopper AsyncTask page
其次,您应该创建一个 class 专用于数据库操作。个人而言,我更喜欢 Apache 的 HttpClient,但如果您更喜欢 HttpURLConnection,请检查 this page。 HttpClient 和 HttpURLConnection 均已定义(以及一些示例)
而且,如果您想发送 JSON 数据,为什么不使用 POST?
最后一件事:如果您在设备上将 wamp 与本地主机一起使用,您的 url 应该是:http://192.168.X.X, but on emulator http://127.0.0.1
希望这对您有所帮助。
我有 Gson 库创建的 JSON 字符串,我想用 HttpURLConnection API "GET" 将这个字符串发送到本地主机服务器 (wamp)。我将 运行 我的 S4 设备上的应用程序通过 USB 电缆连接到我的笔记本电脑。
感谢任何帮助。
我有这个 JSON 字符串:
{
"latitude":80.86907321,
"longitude":15.66542435,
"formatted":"22.04.2015 11:11:00",
"route":4
}
我里面有这个方法 class "MyLocationListener":
private String convertToJSON(double pLong, double pLat, String formatted) {
//envelop the data in JSON format.
Data d = new Data(pLat, pLong, formatted,route_number);
Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new DataSerializer()).create();
return gson.toJson(d);
}
首先,你要了解AsyncTask。
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
这里link给AndroidDevelopper AsyncTask page
其次,您应该创建一个 class 专用于数据库操作。个人而言,我更喜欢 Apache 的 HttpClient,但如果您更喜欢 HttpURLConnection,请检查 this page。 HttpClient 和 HttpURLConnection 均已定义(以及一些示例)
而且,如果您想发送 JSON 数据,为什么不使用 POST?
最后一件事:如果您在设备上将 wamp 与本地主机一起使用,您的 url 应该是:http://192.168.X.X, but on emulator http://127.0.0.1
希望这对您有所帮助。