2


I know it's a strange question but I need one thing: I receive a string as a result of a http operation. In reality this string contains an array of strings like this:

["Leon","Prova 2","TestData"]


Basically I want to convert this string into an ArrayList because I need it for an adapter. This is the java code:

public class Archivio{

    static ArrayList<String> titoli = new ArrayList<String>();
    static ArrayList<String> art = new ArrayList<String>();
    static String response, response2 = null;
    HttpClient httpclient;
     HttpPost httppost,httppost2;
     List<NameValuePair> nameValuePairs,namePairs;
    @SuppressLint({ "NewApi", "NewApi", "NewApi" })
    Archivio() {

        try {

            StrictMode.ThreadPolicy policy = new
                    StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
                    StrictMode.setThreadPolicy(policy);

                    httpclient=new DefaultHttpClient();
                     httppost= new HttpPost("http://tripleleon.altervista.org/artiview2.php"); 
                     nameValuePairs = new ArrayList<NameValuePair>();
                      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     //Esecuzione richiesta HTTP Post e ricezione risultato operazione
                     //response=httpclient.execute(httppost);
                     ResponseHandler<String> responseHandler = new BasicResponseHandler();
                     response = httpclient.execute(httppost, responseHandler);
                     System.out.println("Response : " + response); 
                     httppost2= new HttpPost("http://tripleleon.altervista.org/artiview3.php"); 
                     namePairs = new ArrayList<NameValuePair>();
                      httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     ResponseHandler<String> responseHandler2 = new BasicResponseHandler();
                     response2 = httpclient.execute(httppost2, responseHandler2);
                     System.out.println("Response : " + response2); 

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

    ArrayList<String> retTitle() {
        return response;
    }


    ArrayList<String> retArt() {
        return response2;   
    }
}
2
  • You should not do network operations from the main thread. Commented Apr 4, 2014 at 14:01
  • as I said into another comment: this is a class made for tests. I'm using threads into the real project Commented Apr 4, 2014 at 14:06

2 Answers 2

5

Try with the following code (being stringArray the String that contains your "array"):

ArrayList<String> myList = new ArrayList<String>(Arrays.asList(stringArray.substring(1, stringArray.length() - 1).replaceAll("\"", "").split(",")));

This will remove the first and last characters ([ and ] respectively) and split by the , character. Afterwards, convert it into an ArrayList using the Arrays.asList method.

Sign up to request clarification or add additional context in comments.

2 Comments

"Type mismatch: cannot convert from List<String> to ArrayList<String>"
Simply wrap it into a new ArrayList<String>(...) statement (updated my answer).
1

You can loop through the JSONArray and add it to ArrayList.

ArrayList<String> list = new ArrayList<String>();
JSONArray jr = new JSONArray("string");
for(int i=0;i<jr.length();i++)
{
   String value =(String) jr.get(i);
   list.add(value); 
}

But you are not using Thread or Asynctask for getting the json from the server. Do not use StrictMode in production stage.

Also check your minsdk in manifest @SuppressLint({ "NewApi", "NewApi", "NewApi" }). You have suppressed lint warning

1 Comment

it's a temporary class made for various tests. Of course I'm using threads in the real project ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.