android将信息放入aynctask

时间:2013-07-25 02:40:16

标签: android json android-asynctask

对于AsyncTask,我是新手。所以我需要有人给我一个例子。我的应用程序很简单,非常直接的方式我只需要有人给我一个如何将我的信息放入AsyncTask的例子。

public class MainActivity extends Activity {

    EditText et;
    Button getanswer;

    JSONArray ResultsSet = null; {}

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


        Button getanswer = (Button) findViewById(R.id.button1);
        getanswer.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            new asynctask().execute();

        }

        });
    } 
}

      class asynctask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            return null;

        }}

JSONParser Class

public class JSONParsser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    EditText et;

    public JSONParsser () {
    }

    public JSONObject getJSONFromURI(String uRI) {

        try{
        HttpClient client = new DefaultHttpClient();
        try {
            URI website = new URI("http://example.com=" + et.getText() + "json");
            HttpGet request = new HttpGet();
            request.setURI(website);
            try {
                HttpResponse response = client.execute(request);
            HttpEntity httpEntity = response.getEntity();
            is = httpEntity.getContent();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return jObj;

        }finally{}

    }{

    }}

JSONParser的实例

public class JSONParserInstance {

    JSONParsser jParser = new JSONParsser();

    public String uRI;

    JSONObject json = jParser.getJSONFromURI(uRI);


     private static final String TAG_RESULTS = "ResultsSet";
     private static final String TAG_SUBJECT = "Subject";
     private static final String TAG_NUMANSWERS = "NumAnswers";
     private static final String TAG_QUESTION = "Question";
     private static final String TAG_QUESTION_CONTENT = "Content";
     private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     private static final String TAG_QUESTION_ANSWERS = "Answers";

            JSONArray ResultsSet = null; {

    try {
       ResultsSet = json.getJSONArray(TAG_RESULTS);

       for(int i = 0; i < ResultsSet.length(); i++){
           JSONObject r = ResultsSet.getJSONObject(i);

           String Subject = r.getString(TAG_SUBJECT);
           String NmAnswers = r.getString(TAG_NUMANSWERS);

           JSONObject Question = r.getJSONObject(TAG_QUESTION);
           String Content = Question.getString(TAG_QUESTION_CONTENT);
           String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER);
           String Answers = Question.getString(TAG_QUESTION_ANSWERS);

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

}}}

我真正需要的是一个如何将它放入我的AsyncTask

的示例

1 个答案:

答案 0 :(得分:0)

我会做类似

的事情
    class asynctask extends AsyncTask<String, Void, JSONObject> {

     private static final String TAG_RESULTS = "ResultsSet";
     private static final String TAG_SUBJECT = "Subject";
     private static final String TAG_NUMANSWERS = "NumAnswers";
     private static final String TAG_QUESTION = "Question";
     private static final String TAG_QUESTION_CONTENT = "Content";
     private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     private static final String TAG_QUESTION_ANSWERS = "Answers";

            @Override
            protected JSONObject doInBackground(String... params) {
                JSONParsser jParser = new JSONParsser();

                public String uRI;

                JSONObject json = jParser.getJSONFromURI(uRI);

                return json ;

            }

            @Override
            protected void onPostExecute(JSONObject json) {
                JSONArray ResultsSet = null;
                try {
                    ResultsSet = json.getJSONArray(TAG_RESULTS);
                        .... } catch ...
                }
    }