从URL解析JSON字符串

时间:2014-09-23 14:23:37

标签: android json

我正在使用AsyncTask类开发Android应用程序以从URL获取JSON。

变量Content打印所有JSON原始代码,我希望它被分割,所以我可以将JSON中的所有数据放在不同的TextView中。

Base1i0.class

package info.androidhive.slidingmenu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

public class Base1i0 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base1_0);

        String serverURL = "JSON URL";
        new LongOperation().execute(serverURL);
    }

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

        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(Base1i0.this);

        TextView descripciocat = (TextView) findViewById(R.id.textviewidioma);

        protected void onPreExecute() {
            descripciocat.setText("Output : ");
            Dialog.setMessage("Carregant informació");
            Dialog.show();
        }

        protected Void doInBackground(String... urls) {
            try {
                // Server url call by GET method
                HttpGet httpget = new HttpGet(urls[0]);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                Content = Client.execute(httpget, responseHandler);

            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }
            return null;
        }

        protected void onPostExecute(Void unused) {
            Dialog.dismiss();

            if (Error != null) {
                descripciocat.setText("Output : "+ Error);
            } else {
                descripciocat.setText("Output : "+ Content);
            }
        }
    }
}

JSON代码

[{
        "id":"1",
        "cox":"11111",
        "coy":"22222",
        "tip":"aaaaa",
        "dca":"bbbbb",
        "des":"ccccc",
        "den":"ddddd",
        "dfr":"eeeee",
        "dde":"fffff",
        "hca":"ggggg",
        "hes":"hhhhh",
        "hen":"iiiii",
        "hfr":"jjjjj",
        "hde":"kkkkk",
        "adr":"lllll",
        "tel":"mmmmm",
        "fax":"nnnnn",
        "web":"ooooo",
        "nif":"ppppp",
        "pob":"qqqqq",
        "ext":"rrrrr",
        "pja":"sssss",
        "com":"ttttt",
        "sup":"uuuuu",
        "nur":"vvvvv",
        "urb":"wwwww",
        "urba":"xxxx",
        "sse":"yyyyy",
        "ind":"zzzzz",
        "con":"33333",
        "sag":"44444",
}]

2 个答案:

答案 0 :(得分:0)

JSON是对象数组,包含仅1个对象,包含31个属性。

要从字符串创建JsonArray,您可以执行:JSONArray arr = new JSONArray(string)

要从数组中获取第一个JSONObject,您可以执行JSONObject obj = arr.getJSONObject(0)

要从对象获取属性,您可以String content = obj.getString("cox")等等。

答案 1 :(得分:-1)

首先必须使用GSON反序列化JSON字符串。

  

Gson是一个Java库,可用于将Java对象转换为   他们的JSON表示。它也可以用于转换JSON   字符串到等效的Java对象。 Gson可以使用任意Java   对象包括您没有的预先存在的对象   的源代码。

完成后,您可以使用它来填充不同的TextView

Mykong's post是一个很好的起点。