使用AsyncTask的与传递一个值 [英] Using AsyncTask with passing a value

查看:182
本文介绍了使用AsyncTask的与传递一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直就这个问题和我打了一个点,我不知道该怎么办。我所要做的是使用一个类来下载和解析出一个文件到一个字符串,然后发送到另一个字符串类来解析出JSON的东西。所有零部件本身做工精细,我已经分别测试了一切。我只是不知道如何发送值将JSON解析开始解析。

I have been working on this and I have hit a point where I dont know what to do. What I am trying to do is use one class to download and parse out a file into a string and then send that string to another class to parse out the JSON stuff. All the parts work fine by themselves and I have tested everything separately. I just dont know how to send the value to the Json parses to start the parsing.

所以这是我filedownloader类。

So this is my filedownloader class.

  public class JsonFileDownloader extends AsyncTask<String, Void, String> {
  //used to access the website
  String username = "admin";
  String password = "admin";
  public String ret = "";



  @Override
  protected String doInBackground(String... params) {
      Log.d("Params ", params[0].toString());
      readFromFile(params[0]);
      return ret;
  }

  private String readFromFile(String myWebpage) {

      HttpURLConnection urlConnection = null;

      try {
          //Get the url connection
          URL url = new URL(myWebpage);
          Authenticator.setDefault(new Authenticator() {
              @Override
             protected PasswordAuthentication getPasswordAuthentication()   {
                  return new PasswordAuthentication(username,   password.toCharArray());
            }
          });
          urlConnection = (HttpURLConnection) url.openConnection();

          InputStream inputStream = urlConnection.getInputStream();


          if (inputStream != null) {
              ret = streamToString(inputStream);
              inputStream.close();
              Log.d("Final String", ret);
          }

      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          if (urlConnection != null) {
              urlConnection.disconnect();
          }
          return ret;

      }
  }
  public static String streamToString(InputStream is) throws IOException   {

      StringBuilder sb = new StringBuilder();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
        while ((line = rd.readLine()) != null) {
          sb.append(line);
      }
       return sb.toString();
   }

  public String getJsonData()
  {
      return ret;
   }

}

这工作正常我都没有错误一遍又一遍进行了测试。
接下来是JSON解析器是这样的。

This works fine I have tested it over and over with no errors. The next is the Json parser which is like this.

public class JSONParser {

JSONObject jsonResponse;
String jsonData;

//Consturctor
public JSONParser()
{
    //this.jsonData = jsonData;
    // this.OutputData = outPutData;
}

public void parsesData(String promo,
                       ArrayList<String> pictureHTTP,
                       ArrayList<String> pathHTTP,
                       ArrayList<String> labelText) throws IOException {

    //Build the Json String
    JsonFileDownloader jfd = new JsonFileDownloader();
   // jsonData = String.valueOf(jfd.execute(promo));
    jfd.execute(promo);
    //jfd.getResuts(jsonData);
    //jsonData = jfd.ret;

    Log.d("JsonData String = " , jsonData);


    //Try to parse the data
    try
    {

        Log.d("Jsondata " , jsonData);
        //Creaate a new JSONObject ith the name/value mapping from the JSON string
        jsonResponse = new JSONObject(jsonData);
        //Returns the value mapped by the name if it exists and is a JSONArry
        JSONArray jsonMainNode = jsonResponse.optJSONArray("");

        //Proccess the JSON node
        int lenghtJsonArrar = jsonMainNode.length();
        for (int i = 0; i<lenghtJsonArrar; i++)
        {
            //Get object for each json node
            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            //Get the node values
            //int song_id = Integer.parseInt(jsonChildNode.optString("song_id").toString());
            String picture = jsonChildNode.optString("picture").toString();
            String pathName = jsonChildNode.optString("path").toString();
            String lableName = jsonChildNode.optString("label".toString());
            //Debug Testing code
            pictureHTTP.add(picture);
            pathHTTP.add(pathName);
            labelText.add(lableName);


        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

现在我知道问题出在哪里发生。当我尝试所以它是空,而且系统无法分配一个值,它永远不会被分配jsonData。
我有jfd.exicute(后尝试了一些东西),但我只是不知道如何从最终的字符串输出到jsonData价值。
感谢您的任何帮助。

Now I know where the problem is occurring. When i try to assign a value to the jsonData it never is assigned so it is null and the system fails. I have tried a few things after the jfd.exicute() but I just dont know how to get the value from the final string output into the jsonData. Thank you for any help with this.

推荐答案

好吧,这里是使用的AsyncTask下载Web内容,并从中得到结果返回到UI线程的整体使用率pretty灵活的模式

Alright, here is a pretty flexible pattern for the overall usage of using AsyncTask to download web content and getting the results from it back to the UI thread.

第1步定义将充当AsyncTask的和你想要的数据,其中之间的消息总线的接口。

Step 1 Define an interface that will act as a message bus between the AsyncTask and where you want the data.

public interface AsyncResponse<T> {
    void onResponse(T response);
}

第2步创建通用AsyncTask的扩展,将采取任何URL,并从中返回结果。你基本上有这个了,但我做了一些调整。最重要的是,允许AsyncResponse回调接口的设置。

Step 2 Create a generic AsyncTask extension that will take any URL and return the results from it. You basically had this already, but I made some tweaks. Most importantly, allowing the setting of the AsyncResponse callback interface.

public class WebDownloadTask extends AsyncTask<String, Void, String> {

    private AsyncResponse<String> callback;

    // Optional parameters
    private String username;
    private String password;

    // Make a constuctor to store the parameters
    public WebDownloadTask(String username, String password) {
        this.username = username;
        this.password = password;
    }

    // Don't forget to call this
    public void setCallback(AsyncResponse<String> callback) {
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params) {
        String url = params[0];
        return readFromFile(url);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (callback != null) {
            callback.onResponse(s);
        } else {
            Log.w(WebDownloadTask.class.getSimpleName(), "The response was ignored");
        }
    }

    /******* private helper methods *******/

    private String streamToString(InputStream is) throws IOException {

        StringBuilder sb = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    private String readFromFile(String myWebpage) {

        String response = null;
        HttpURLConnection urlConnection = null;

        try {
            //Get the url connection
            URL url = new URL(myWebpage);

            // Unnecessary for general AsyncTask usage
            /* 
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password.toCharArray());
                }
            });
            */

            urlConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = urlConnection.getInputStream();

            if (inputStream != null) {
                response = streamToString(inputStream);
                inputStream.close();
                Log.d("Final String", response);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }

        return response;
    }
}

第3步你出来,并使用AsyncTask的,无论你的愿望。下面是一个例子。请注意,如果你不使用 setCallback ,您将无法获得来自AsyncTask的传来的数据。

Step 3 Go forth and use that AsyncTask wherever you wish. Here is an example. Note that if you do not use setCallback, you will be unable to get the data that came from the AsyncTask.

public class MainActivity extends Activity {

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

        WebDownloadTask task = new WebDownloadTask("username", "password");
        task.setCallback(new AsyncResponse<String>() {
            @Override
            public void onResponse(String response) {
                // Handle response here. E.g. parse into a JSON object
                // Then put objects into some list, then place into an adapter... 
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
            }
        });

        // Use any URL, this one returns a list of 10 users in JSON
        task.execute("http://jsonplaceholder.typicode.com/users");
    }
}

这篇关于使用AsyncTask的与传递一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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