简体   繁体   English

如何从Android中返回XML或JSON的URL获取数据

[英]How to get data from a url that returns XML or JSON in Android

I'm new in Android, but i worked with XML parsers in android to parse RSS in my app. 我是Android的新手,但是我在android中使用XML解析器来解析我的应用程序中的RSS。 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. 我开发了Web API的服务器端,对其进行了测试,到目前为止,它仍在工作。 So when I call a url like this 所以当我这样叫一个URL

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

I get this XML (and JSON as needed). 我得到了这个XML(以及所需的JSON)。

<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. 现在,我想调用该URL并接收XML或JSON字符串,然后将其解析为数组或列表。 How can I do this? 我怎样才能做到这一点?

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 https://github.com/ogrebgr/android_volley_examples

or 要么

you can use Asynctask to get the JSON data from the server. 您可以使用Asynctask从服务器获取JSON数据。

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) {}
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM