如何在另一个活动中将asnycktask中的字符串数组返回给String []

时间:2017-04-24 22:31:13

标签: java android android-asynctask

抱歉,我认为我的问题太混乱,代码太多,所以我正在编辑它并缩短它。

所以基本上我创建了一个名为BackgroundWorker.java的外部Asynctask,然后在另一个名为picture.java的活动中调用它。

好的,下面是我的AsyncTask代码(backgroundWorker.java)。我能够获取JSON文件并获取提取Json.getString [' image_link']并且它显示在alertdialog中。我想将Json.getString [' image_link']的值复制到私有String [] mStrings =中,它们位于另一个名为picture.java的活动中。

    if (type.equals("login")){
        try {
            String username = params[1];
            String password = params[2];
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
                    + URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            // execute the data
            bufferedWriter.flush();
            // close all connection
            bufferedWriter.close();
            outputStream.close();

            // get the return
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String result="";
            String line="";
            while((line = bufferedReader.readLine()) != null){
                result += line;
                sb.append(line+"\n");
                String finalJson = sb.toString();

                JSONObject parentobject = new JSONObject(finalJson);
                JSONArray parentArray = parentobject.getJSONArray("people");
                stringbuff = new StringBuffer();


                for(int i = 0; i < parentArray.length(); i++){
                    JSONObject finalObject = parentArray.getJSONObject(i);
                    placename = finalObject.getString("name");
                    placelink = finalObject.getString("image_link");
                    stringbuff.append(placename + "  " + placelink + "," + "\n");

                }


            }

            // close buffered reader
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return sb.toString();

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

    }
@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
}

picture.java有一个名为mString = private String [] mStrings的变量。它没有任何内容,我想将Json.getString [&#39; image_link&#39;]的值复制到其中。我不知道该怎么做请帮忙。我真的需要这样做

0 个答案:

没有答案