0

我在我的主要活动中调用异步任务类:这里是代码

 public class MainActivity extends Activity implements AsyncResponse {

                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);



                        Connection connection=new Connection();
                        connection.execute();



                    }
        Here is my Connection class:
        class  Connection extends AsyncTask<String,String, Void>
        {
            public AsyncResponse delegate=null;
            String result = "";
             InputStream is=null;
            @Override
            protected Void doInBackground(String... params) {


                // TODO Auto-generated method stub
                /*ArrayList<NameValuePair> nameValuePairs = null;
                  int i=0;

                     String username=params[i].toString();
                     String password=params[i+1].toString();
                     String validation=params[i+2].toString();
                  nameValuePairs = new ArrayList<NameValuePair>();
                 nameValuePairs.add(new BasicNameValuePair("username",username));
                 nameValuePairs.add(new BasicNameValuePair("password",password));

               //  nameValuePairs.add(new BasicNameValuePair("username",));*/
                try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://10.0.2.2/connection.php");
                      //  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                         is = entity.getContent();
                }catch(Exception e){
                        Log.e("log_tag", "Error in http connection "+e.toString());
                }

                try{
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                        }
                        is.close();

                        result=sb.toString();
                }catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                }
                return null;
                // TODO Auto-generated method stub
                }
            protected void onPostExecute(Void v) {

             try{


                 JSONArray jArray = new JSONArray(result);

                 delegate.processFinish(jArray);

               //  labels2.add(password);


               //Returndata(labels2); 

             }

             catch(JSONException e){
                 Log.e("log_tag", "Error parsing data "+e.toString());
         }
        }




        }

在执行后,我将 Jarray 发送到一个接口:并在我的主 Activity 中使用该接口:这是我的接口:

public interface AsyncResponse {

            void processFinish(JSONArray jArray);

        }

并像这样使用它的主要活动:

 @Override
        public void processFinish(JSONArray jarray) {
            // TODO Auto-generated method stub
            try {
                for(int i=0;i<=jarray.length();i++)
                   {
                             JSONObject json_data;
                json_data = jarray.getJSONObject(i);
                String  username=json_data.getString("username");
                 String password=json_data.getString("password");
                 Toast.makeText(getBaseContext(),username+password,Toast.LENGTH_LONG).show();
            }

                 }
                catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
4

2 回答 2

3

只是一个建议当您的 doInBackground 方法返回 String 时,onPostExecute 中的参数将具有 doInBackground 的返回值。您不必声明单独的字符串。往下看,

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected String doInBackground(URL... urls) {
         String someresult = "got from some operations";
         return someresult;
     }

     protected void onPostExecute(String result) {
         System.out.println("The resulting string from doInBackground is " + result);
     }
 }

其次,您拥有的 Connection 课程,

public AsyncResponse delegate=null;

您还没有初始化委托变量,它为空!!所以你有一个空指针异常。你永远不能在java中实例化一个接口。但是,您可以通过接口的类型来引用实现接口的对象。希望这可以帮助。

编辑:

如果你有一个由 B 类实现的接口,那么你可以AsyncResponse test = new B();像下面这样

public interface AsyncResponse 
{
}
public class B implements AsyncResponse 
{
}

public static void main(String[] args)
{
    AsyncResponse  test = new B();
}

现在在 Android 中你不能实例化一个 Activity。而是引用该活动实例。我敢打赌你那里有方法。

于 2013-02-23T10:17:00.457 回答
0

我认为代码没有声明正在使用的接口。

在 MainActivity 类中应该声明下面的内容。

您创建的连接线程没有声明任何接口。

因此它将被声明为空值。

        public class MainActivity extends Activity implements AsyncResponse {

            Connection connection=new Connection(); // You shud declare on top

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                    connection.delegate=this;

                    super.onCreate(savedInstanceState);

                    setContentView(R.layout.activity_main);


                    connection.execute();



                }
于 2014-03-29T03:48:40.413 回答