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.

Expedito
  • 7,771
  • 5
  • 30
  • 43
Nidhin Rejoice
  • 1,367
  • 1
  • 16
  • 26

3 Answers3

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.

LanternMike
  • 664
  • 1
  • 5
  • 16
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

Jordan Doyle
  • 2,976
  • 4
  • 22
  • 38
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

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28
  • JSONArray jArray = new JSONArray(result); This line gives error...String can't be converted into JsonArray – Nidhin Rejoice May 19 '13 at 10:42
  • 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. – Niraj Adhikari May 19 '13 at 11:16
  • 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. – Nidhin Rejoice May 19 '13 at 14:45