求大神解释一下这段Android代码,全部。谢谢
Handlerhandler=newHandler(){@OverridepublicvoidhandleMessage(Messagemsg){if(msg.what=...
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what==0x123)
{
String resStr = msg.obj.toString().trim();
if(resStr.equals("ok"))
{
Toast.makeText(MainActivity.this,"succ" , Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(MainActivity.this,"error" , Toast.LENGTH_SHORT).show();
}
JSONObject response;
try {
response = new JSONObject(msg.obj.toString().trim());
if(response.getBoolean("Msg"))
{
Toast.makeText(MainActivity.this,"succ" , Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this,"error" , Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
super.handleMessage(msg);
}
};
public void doPostCmd(final String url,final String dataStr) {
new Thread(new Runnable() {
@Override
public void run() {
MyHttpClient server = new MyHttpClient();
Message msg = new Message();
msg.obj = server.post(url, dataStr);
msg.what = 0x123;
handler.sendMessage(msg);
}
}).start();
}
protected void post(final String url) {
} 展开
@Override
public void handleMessage(Message msg) {
if (msg.what==0x123)
{
String resStr = msg.obj.toString().trim();
if(resStr.equals("ok"))
{
Toast.makeText(MainActivity.this,"succ" , Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(MainActivity.this,"error" , Toast.LENGTH_SHORT).show();
}
JSONObject response;
try {
response = new JSONObject(msg.obj.toString().trim());
if(response.getBoolean("Msg"))
{
Toast.makeText(MainActivity.this,"succ" , Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this,"error" , Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
super.handleMessage(msg);
}
};
public void doPostCmd(final String url,final String dataStr) {
new Thread(new Runnable() {
@Override
public void run() {
MyHttpClient server = new MyHttpClient();
Message msg = new Message();
msg.obj = server.post(url, dataStr);
msg.what = 0x123;
handler.sendMessage(msg);
}
}).start();
}
protected void post(final String url) {
} 展开
2个回答
2018-10-23 · 百度知道合伙人官方认证企业
关注
展开全部
doPostCmd()方法发送消息给handler,handler接受信息执行相应命令
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询