0

I'm new in Android, but i worked with XML parsers in android to parse RSS in my app. Now I want to send and receive new app-data to/from my server.

I developed server side of my web API, tested it and so far it is working. So when I call a url like this

http://URL/api/getQuery/value1/value2

I get this XML (and JSON as needed).

<ArrayOfstring>
  <string>4.5</string>
  <string>Glu Mobile</string>
  <string>4.2.0</string>
  <string>1392/07/18</string>
  <string>500</string>
  <string>112MB</string>
  <string>free</string>
  <string>creator</string>
  <string>describtion</string>
  <string>similarapp</string>
</ArrayOfstring>

Now I want to call that url and receive XML or JSON string and then parsing it into an array or a list. How can I do this?

1 Answer 1

1

Use volley Library...It will be very useful to you..here is the link for you...Download and examine that Be patience while you are doing the first time after that it will be very easy...

https://github.com/ogrebgr/android_volley_examples

or

you can use Asynctask to get the JSON data from the server.

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class AsyncTaskActivity extends Activity implements OnClickListener {

Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn = (Button) findViewById(R.id.button1);
    // because we implement OnClickListener we only have to pass "this"
    // (much easier)
    btn.setOnClickListener(this);
}

public void onClick(View view) {
    // detect the view that was "clicked"
    switch (view.getId()) {
    case R.id.button1:
        new LongOperation().execute("");
        break;
    }
}

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
         String url = params[0];

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);           
        request.setHeader("Content-Type", "text/xml");
        HttpResponse response;
        try {
            response = httpClient.execute(request);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        // Result is in String Format
        // you can use JSON api to convert into JSONObject

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

}

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

Comments

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.