接收来自服务器的响应后,完成的活动 [英] Finish the activity after receiving response from server

查看:93
本文介绍了接收来自服务器的响应后,完成的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成的活动时,收到了来自服务器的另一个Java类的完全缓解。

Category.java

这是主要的活动

 公共无效onListItemClick(ListView的父,视图V,INT位置,长ID)
    {
        //获取选定的类别ID和放大器;名称。
        字符串CATID =;
        字符串catName =;
        的LinearLayout LL =(的LinearLayout)findViewById(R.id.linearLayout1);
        查看查看= ll.getChildAt(0);
        如果(查看的instanceof的ListView){
            ListView控件LVIEW =(ListView控件)视图;
            RowData rowData =(RowData)lView.getAdapter()的getItem(位置)。
            CATID = rowData.mCatId;
            catName = rowData.mTitle;
        }

        字符串URL =htt​​p://global.thinlenses.co.uk/virtualMirror/productlisting.php;
        串的xml =&其中;?xml的版本= \1.0 \编码= \utf-8的\>? +<类别><编号>中+ CATID +< / ID>中
                    +<数据>中+的plist+< /数据>< /类>中;

        如果(getIntent()。getBooleanExtra(虚拟机,FALSE))
            新SpinnerHelper()serverCall(Category.this,ProductListVM,URL,XML,catName);
        其他
            新SpinnerHelper()serverCall(Category.this,产品列表,网址,XML,catName)。
    }
 

SpinnerHelper.java随班就读不活动

 无效serverCall(上下文的背景下,最终的目标字符串,最后字符串URL,最终的XML字符串,字符串的ExtraValue)
    {
        尝试 {
            this.context =背景;
            this.target =目标;
            this.extraValue =的ExtraValue;

            ConnectivityManager赌侠=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            如果(conMan.getActiveNetworkInfo()= NULL和放大器;!&安培; conMan.getActiveNetworkInfo()isConnected())
            {
                对话框=新ProgressDialog(上下文);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(真正的);
                dialog.setTitle(R.string.app_name);
                dialog.setMessage(context.getString(R.string.progressing));
                dialog.show();

                //用于更新微调创建一个线程。
                螺纹progressThread =新主题(新的Runnable(){
                    公共无效的run(){
                        尝试 {
                            而(!isExecuted){
                                视频下载(1000); //每次更新之间等待1000毫秒。
                                handler.sendMessage(handler.obtainMessage()); //主动更新处理。
                            }
                        }
                        赶上(InterruptedException异常E){
                            Log.e(虚拟机,异常而微调器更新为:+ e.getMessage());
                        }
                    }
                });
                progressThread.start();

                //创建一个线程来获得服务器的响应。
                螺纹registerThread =新主题(新的Runnable(){
                    公共无效的run()
                    {
                        //获取响应XML从服务器解析它。
                        字符串resXML =新的连接()GETRESPONSE(URL,XML)。
                        XMLParser的分析器=新XMLParser的();
                        文档DOC = parser.getDomElement(resXML);
                        如果(文件!= NULL)
                        {
                            如果(target.equals(类别)){
                                节点列表节点列表= doc.getElementsByTagName(类);
                                名称=新的String [nodeList.getLength()];
                                ID =新的String [nodeList.getLength()];

                                //获取所有节点的值,并存入数组。
                                对于(INT指数= 0;指数< nodeList.getLength();指数++)
                                {
                                    element元素=(元)nodeList.item(指数);
                                    ID [指数] = parser.getValue(元素,ID);
                                    名称[索引] = parser.getValue(元素,类别名称);
                                }
                                结果=是;
                            }
    }
 

}

该处理器将被调用时,我收到来自服务器的完整效应初探。

 处理程序处理程序=新的处理程序()
  {
    公共无效的handleMessage(消息消息)
    {
        如果(isExecuted)
        {
            dialog.dismiss();
            isExecuted = FALSE;

            如果(result.equals(是))
            {
                  /////在这里,我要开始下一个活动并完成类别(previous活动)。

             }
 

解决方案

您可以使用此上下文参考

 意向意图=新的意图(背景下,nextActivity.class);
context.startActivity(意向);
((活动)上下文).finish();
 

I want to finish the activity when received complete response from server in another java class.

Category.java

This is main activity

public void onListItemClick(ListView parent, View v, int position, long id)
    {
        // Get the selected category id & name.
        String catId = "";
        String catName = "";
        LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
        View view = ll.getChildAt(0);
        if(view instanceof ListView) {
            ListView lView = (ListView) view;
            RowData rowData = (RowData) lView.getAdapter().getItem(position);
            catId = rowData.mCatId;
            catName = rowData.mTitle;
        }

        String url = "http://global.thinlenses.co.uk/virtualMirror/productlisting.php"; 
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<category><Id>" + catId + "</Id>"
                    + "<Data>" + "pList" + "</Data></category>";

        if(getIntent().getBooleanExtra("VM", false))
            new SpinnerHelper().serverCall(Category.this, "ProductListVM", url, xml, catName);
        else
            new SpinnerHelper().serverCall(Category.this, "ProductList", url, xml, catName);
    }

SpinnerHelper.java Regular Class not Activity

void serverCall(Context context, final String target, final String url, final String xml, String extraValue)
    {
        try {
            this.context = context;
            this.target = target;
            this.extraValue = extraValue;

            ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if(conMan.getActiveNetworkInfo() != null && conMan.getActiveNetworkInfo().isConnected())
            {
                dialog = new ProgressDialog(context);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(true);
                dialog.setTitle(R.string.app_name);
                dialog.setMessage(context.getString(R.string.progressing));
                dialog.show();

                // Create a thread for updating the spinner.
                Thread progressThread = new Thread(new Runnable() {
                    public void run() {
                        try {
                            while(!isExecuted) {
                                Thread.sleep(1000);                                 // Wait 1000ms between each update.
                                handler.sendMessage(handler.obtainMessage());       // Active the update handler.
                            }
                        }
                        catch (InterruptedException e) {
                            Log.e("VM", "Exception while spinner is updating at: " + e.getMessage());
                        }
                    }
                });
                progressThread.start();

                // Create a thread to get response from server.
                Thread registerThread = new Thread(new Runnable() {
                    public void run()
                    {
                        // Get response XML from server and parse it.
                        String resXML = new Connection().getResponse(url, xml);
                        XMLParser parser = new XMLParser();
                        Document doc = parser.getDomElement(resXML);
                        if(doc != null)
                        {
                            if(target.equals("Category")) {
                                NodeList nodeList = doc.getElementsByTagName("Category");
                                name = new String[nodeList.getLength()];
                                id = new String[nodeList.getLength()];

                                // Fetch all node values and store into arrays.
                                for(int index = 0; index < nodeList.getLength(); index++)
                                {
                                    Element element = (Element) nodeList.item(index);
                                    id[index] = parser.getValue(element, "id");
                                    name[index] = parser.getValue(element,"CategoryName");
                                }
                                result = "yes";
                            }
    }

}

This handler is called when i recieved complete reponse from server.

  Handler handler = new Handler()
  {
    public void handleMessage(Message message)
    {
        if(isExecuted)
        {
            dialog.dismiss();
            isExecuted = false;

            if(result.equals("yes"))
            {
                  /////HERE i want to start the next activity and finish the Category(previous activity).

             }

解决方案

you can use your context reference for this

Intent intent = new Intent(context,nextActivity.class);
context.startActivity(intent);
((Activity) context).finish();

这篇关于接收来自服务器的响应后,完成的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆