gpt4 book ai didi

java - 如何在 Android 上 pretty-print JSON 原始数据

转载 作者:行者123 更新时间:2023-11-29 19:45:58 25 4
gpt4 key购买 nike

<分区>

我有一个 JSON 格式的 API,我的 json 数据在我的 android 应用程序上显示为原始数据,我希望它们以像这样的 pretty-print 格式显示

Pretty JSON Data

不是这样

JSON Raw display on Android

这是我的java代码:

package com.example.cbmedandroid;
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.EditText;
public class MainActivity extends Activity implements OnClickListener {
@Override


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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("http://10.0.2.2:8000/api/horaire.json");
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) {
EditText et = (EditText)findViewById(R.id.my_edit);


et.setText(results);


}


Button b = (Button)findViewById(R.id.my_button);


b.setClickable(true);
}


}
}

怎么做?

谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com