从Android中,用户界面服务发送JSON数据 [英] Send JSON data from a service to UI in Android
问题描述
要求是:我有一个后台服务,并在该服务中,我做一个REST调用来获取一个JSON数据。我想对JSON数据发送到用户界面和更新内容。
我可以用即存储整个JSON字符串中的共享preferences和检索的用户界面,但我不认为这是有效的。一种方法
任何想法的家伙?我已经在用户界面增加了一个处理程序来更新内容,但我不熟悉使用它。
样品REST调用code:
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(DATA_URL);
httpPost.setHeader(内容类型,应用/ JSON);
//将结果传递给一个字符串生成器/实体
StringEntity SE = NULL;
尝试 {
本身=新StringEntity(RequestJSON.toString());
}赶上(UnsupportedEncodingException E){
e.printStackTrace();
}
//设置POST请求的结果字符串
httpPost.setEntity(SE);
//手柄什么从页返回的
ResponseHandler的ResponseHandler的=新BasicResponseHandler();
尝试 {
HTT presponse响应=(HTT presponse)httpClient.execute(httpPost,ResponseHandler所);
//响应将有我需要的UI更新JSON数据
//显示通知
showNotification(更新完成);
}赶上(UnsupportedEncodingException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
} 最后 {
// HttpClient的= NULL;
}
在界面活性
处理程序将myHandler =新的处理程序(){
@覆盖
公共无效的handleMessage(信息MSG){
捆绑Recevied = msg.getData();
字符串RESP = Recevied.getString(MKEY);
}
};
信使=新使者(将myHandler);
}
通过信使到服务,一旦结果就绪:
信息味精= Message.obtain();
捆绑数据=新包();
data.putString(MKEY
SMSG);
msg.setData(数据);
尝试 {
//将消息发送回客户端活动。
messenger.send(MSG);
}赶上(RemoteException的E){
e.printStackTrace();
}
The requirement is : I have a background service and in that service I am doing a REST call to get a JSON data. I want to send the JSON data to UI and update contents.
One method I can use i.e. store the entire JSON string in SharedPreferences and retrieve in UI but I don't think that's efficient.
Any idea guys ? I have added a Handler in UI to update elements but I am not that familiar in using it.
Sample REST call code :
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(DATA_URL);
httpPost.setHeader("Content-Type", "application/json");
//passes the results to a string builder/entity
StringEntity se = null;
try {
se = new StringEntity(RequestJSON.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//sets the post request as the resulting string
httpPost.setEntity(se);
//Handles what is returned from the page
ResponseHandler responseHandler = new BasicResponseHandler();
try {
HttpResponse response = (HttpResponse) httpClient.execute(httpPost, responseHandler);
// response will have JSON data that I need to update in UI
//Show notification
showNotification("Update Complete");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// httpClient = null;
}
On UI activity
Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle Recevied = msg.getData();
String resp = Recevied.getString("Mkey");
}
};
messenger = new Messenger(myHandler);
}
pass the messanger to service and once result ready:
Message msg = Message.obtain();
Bundle data = new Bundle();
data.putString("Mkey",
Smsg);
msg.setData(data);
try {
// Send the Message back to the client Activity.
messenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
这篇关于从Android中,用户界面服务发送JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!