2

I get the following response after executing a HTTP post from PHP in Android.

new Array("1:Excellent","2:Good","3:Average","4:Not good","5:Disappointing")

I want to parse this result to a string array, so that I can fill a spinner with these values.

7
  • Can you change the format of the data sent by the server? If so JSON would be a good format. Also, what does the server sent, if the description contains an " character? Commented May 19, 2013 at 10:23
  • Your server sent you javascript code, which is probably intended to be evaluated using javascript's eval function. This is generally considered unsafe, especially if you develop an non-internal application. Commented May 19, 2013 at 10:29
  • actually I don't have access to the php. All I could do is Android. Commented May 19, 2013 at 10:36
  • Have a look at stackoverflow.com/questions/7487908/… Commented May 19, 2013 at 10:37
  • possible duplicate of parse String with Regex Commented May 19, 2013 at 11:33

3 Answers 3

0

I dont see a string constructor that can take an array but maybe check the Java String documentation. You could check the Array's size, create a string array of that size, then manually copy in from Array to the String array via a loop value by value. I am not as familiar with copying from an Array object but it would seem you would get the object and typecast to a string. Also i'm not sure i see Array in java collections, i use a few languages, so if you have not managed to get the data into a java object yet this would not yet apply.

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

3 Comments

I don't think that this line is part of his source code but rather the response of the server.
ya its the response from the server
in the java code you create a new Array? this would probably apply then i wasn't sure if Array was in the javascript/php or java.
0
private void parseJson() {
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();
    }

    // response to inputstream
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"));

        sb = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();

        result = sb.toString();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                .show();
    }
    // to string
    try {
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data = null;
        for (int i = 0; i < jArray.length(); i++) {
            json_data = jArray.getJSONObject(i);

            b.add(json_data.getString("Name"));

        }

    }

Declare necessary variables ! use this inside doInBackGround() method of asynctask

3 Comments

JSONArray jArray = new JSONArray(result); This line gives error...String can't be converted into JsonArray
it might mean that the response that you are getting from the url is incorrect...in the catch block after the try block under json exception ... put a line e.printStackTrace(); ..you should discover why the exception has been caught.
I am getting the above error in the catch block. The php is not sending back response in Json format. Thats the problem here. It is echoing just the array.
0

You could use ScriptEngine with the engine set to Javascript, your code doesn't throw any errors within JS.

No errors

ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript").getEngineByName("JavaScript");

String expression = ...; // Get the expression from the web server
String ss[] = (String[]) js.eval(expression);

ScriptEngine is available from JDK6+ you can find the docs here. You can include javax.script in your Android app, you can find the source for that here

3 Comments

ScriptEngine is not available in Android.
EDIT: Could you not include javax.script package with your Android app? You can get it all from grepcode.com - I'll include this in my answer.
finally I found a solution by tokenizing the response using delimiters, it is not the solution I really wanted. But for the time being I am using it.

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.