JSON解析结果的Andr​​oid [英] Parsing JSON Result Android

查看:204
本文介绍了JSON解析结果的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了JSON格式接收的一些数据的Andr​​oid应用程序。目前的结果非常难看。我想在我的应用程序的多个TextViews整齐地显示此数据,但我不知道如何去做。下面来源$ C ​​$ C。

Main.java

 进口java.io.IOException异常;
进口的java.io.InputStream;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.protocol.BasicHttpContext;
进口org.apache.http.protocol.HttpContext;进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;公共类主要活动扩展实现OnClickListener {    TextView的TXT1;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(本);
    }    @覆盖
    公共无效的onClick(查看为arg0){
        按钮B =(按钮)findViewById(R.id.my_button);
        b.setClickable(假);
        新LongRunningGetIO()执行();
    }    私有类LongRunningGetIO扩展的AsyncTask<太虚,太虚,字符串> {        保护字符串getASCIIContentFromEntity(HttpEntity实体)抛出IllegalStateException异常,IOException异常{
           InputStream的时间= entity.getContent();
             StringBuffer的出=新的StringBuffer();
             INT N = 1;
             而(N 0){
                 字节[] B =新的字节[4096];
                 N = in.read(B);
                 如果(N 0)out.append(新字符串(B,0,N));
             }
             返回out.toString();
        }        @覆盖
        保护字符串doInBackground(虚空...... PARAMS){
             HttpClient的HttpClient的=新DefaultHttpClient();
             HttpContext的localContext =新BasicHttpContext();
             HTTPGET HTTPGET =新HTTPGET(https://api.myjson.com/bins/363s3);
             字符串文本= NULL;
             尝试{
                   HTT presponse响应= httpClient.execute(HTTPGET,localContext);
                   HttpEntity实体= response.getEntity();
                   文字= getASCIIContentFromEntity(实体);
             }赶上(例外五){
                 返回e.getLocalizedMessage();
             }
             返回文本;
        }        保护无效onPostExecute(字符串结果){
            如果(结果!= NULL){
                TXT1 =(的TextView)findViewById(R.id.textView1);
                txt1.setText(结果);
            }
            按钮B =(按钮)findViewById(R.id.my_button);
            b.setClickable(真);
        }
    }
}


解决方案

怎么样努力类似,

 的JSONObject的JSONObject =新的JSONObject(jsonString);

顺便说一句,排球是任何一种请求好的库。

I am building an Android application that receives some data in JSON format. At the moment the result is very ugly. I want to display this data neatly in multiple TextViews in my application but I am not to sure how to do it. Source code below.

Main.java

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

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

public class Main extends Activity implements OnClickListener {

    TextView txt1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button)findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

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

        protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
           InputStream in = entity.getContent();
             StringBuffer out = new StringBuffer();
             int n = 1;
             while (n>0) {
                 byte[] b = new byte[4096];
                 n =  in.read(b);
                 if (n>0) out.append(new String(b, 0, n));
             }
             return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
             HttpGet httpGet = new HttpGet("https://api.myjson.com/bins/363s3");
             String text = null;
             try {
                   HttpResponse response = httpClient.execute(httpGet, localContext);
                   HttpEntity entity = response.getEntity();
                   text = getASCIIContentFromEntity(entity);
             } catch (Exception e) {
                 return e.getLocalizedMessage();
             }
             return text;
        }   

        protected void onPostExecute(String results) {
            if (results!=null) {
                txt1 = (TextView)findViewById(R.id.textView1);
                txt1.setText(results);
            }
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }
}

解决方案

What about trying something like,

JSONObject jsonObject = new JSONObject(jsonString);

By the way, Volley is a good library for any kind of requests.

这篇关于JSON解析结果的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆