编舞者跳过x帧!应用程序可能在其主线程上做了太多工作

时间:2014-12-30 17:54:28

标签: java android multithreading android-activity android-asynctask

我不明白为什么会发生这种情况? 有人有想法吗? 我正在尝试发送&从PHP n接收数据动态附加xml布局中的数据。 任何人有任何想法?当我使用asynctask将数据发送到PHP时,这些东西运行良好。 但是当我试图接收并附加这个logcat给了我哎。

ActivityMain.java

package com.example.myweb;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;


import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.renderscript.Element;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

 public class MainActivity extends Activity {
    Button button;
    EditText emailBox;
    EditText passwordBox;
    String emailId;
    String passwordId;
    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
    @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        button = (Button) findViewById(R.id.login1);
        emailBox   = (EditText)findViewById(R.id.email);
        passwordBox   = (EditText)findViewById(R.id.password);

       button.setOnClickListener(new Button.OnClickListener()   {             
           public void onClick(View v)  {               
            try {
               new toPHP(MainActivity.this).execute();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();                    
            }               
           }  
         });
       }
    public void afterEffect(String str){

        TextView textV1 = (TextView)findViewById(R.id.textV1);
        textV1.setText(str);        

    }
    public void showChatList(JSONArray json){

        /*
        Document doc;
        String fileloc = "C:\\Users\\Admin\\workspace\\myWeb\\res\\layout\\activity_main.xml";
        try {
            doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(fileloc));
            //Node nR = doc.getElementById("mainR");
            removeChilds(nR);
        } catch (SAXException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        */
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainR);
        for (int i = 0; i < json.length(); i++) { 
            try {
                JSONObject c = json.getJSONObject(i);
                LinearLayout LL = new LinearLayout(this);
                ImageView pp = new ImageView(this);
                TextView name = new TextView(this);
                TextView username = new TextView(this);
                int generatedId = generateViewId();
                pp.setId(generatedId);
                LL.setOrientation(LinearLayout.HORIZONTAL);
                new ImageLoadTask(pp).execute("http://117.99.55.83/"+c.getString("img"));
                name.setText(c.getString("name"));
                username.setText(c.getString("username"));
                LL.addView(pp);
                LL.addView(name);
                LL.addView(username);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    }
    public static void removeChilds(Node node) {
        while (node.hasChildNodes())
            node.removeChild(node.getFirstChild());
    }
    public static int generateViewId() {
        for (;;) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }   
}

toPHP

package com.example.myweb;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

public class toPHP extends AsyncTask<Object, Object, JSONArray>{
    final MainActivity main;

    public toPHP(MainActivity main) {
        this.main = main;
    }

    private JSONParser jsonParser = new JSONParser();
    String email,password;
    EditText emailBox;
    EditText passwordBox;
    @Override
    protected void onPreExecute() {
         super.onPreExecute();
        TextView textV1 = (TextView)main.findViewById(R.id.textV1);
        ProgressBar spinner = (ProgressBar)main.findViewById(R.id.spinner);
        spinner.setVisibility(View.VISIBLE);    
        textV1.setText("Reaching em!!");        
    }   
    @Override
    protected JSONArray doInBackground(Object... v) {


        //main = (MainActivity)parameters[0];
        //main.afterEffect("sending...");
        emailBox = (EditText) main.findViewById(R.id.email);
        passwordBox = (EditText) main.findViewById(R.id.password);
        email = emailBox.getText().toString();
        password = passwordBox.getText().toString();            
        JSONArray json = null;
        json = getUserLoggedIn(email, password);
        //main.afterEffect("drawing");
        return json;        
     }


    public JSONArray getUserLoggedIn(String email,String password){
        JSONArray json = null;
        /*
        HttpClient client = new DefaultHttpClient();
        HttpPost  post = new HttpPost("http://localhost/testand.php");
        */
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("email", email));
        pairs.add(new BasicNameValuePair("password", password));
        //post.setEntity(new UrlEncodedFormEntity(pairs));
        //HttpResponse response = client.execute(post);
        //HttpEntity resEntity = response.getEntity();

        //if (resEntity != null) {

            //String responseStr = EntityUtils.toString(resEntity).trim();

            json = jsonParser.getJSONFromUrl("http://117.99.55.83/resource/android/CHATS.php", pairs);



        //}     
        return json;
    }

    protected void onPostExecute(JSONArray json) {
        main.showChatList(json);
    }

}

logcat的

12-30 17:47:34.418: I/Choreographer(736): Skipped 41 frames!  The application may be doing too much work on its main thread.
12-30 17:47:37.221: D/dalvikvm(736): GC_CONCURRENT freed 272K, 13% free 2757K/3168K, paused 77ms+79ms, total 303ms

0 个答案:

没有答案