gpt4 book ai didi

java - Android:对 JSON 进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:18 29 4
gpt4 key购买 nike

刚开始使用 REST API 和 JSON 文件,但我已经从天气 API 检索了有关我当前位置天气状况的数据。 JSON 文件包含数据,例如我的位置、天气速度等。我希望将数据的所有这些单独部分分类到 TextView 中,以便可以清楚地看到它们。

我的异步类:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import java.util.ArrayList;
import android.widget.*;
import java.util.Date;
import android.util.Log;

public class RESTAPI extends Activity {

ArrayList<String> items = new ArrayList<String>();
// json test string
String jsonTest;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restapi);
// start the AsyncTask for calling the REST service using httpConnect class
new AsyncTaskParseJson().execute();
}


// added asynctask class methods below - you can make this class as a separate class file
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

// set the url of the web service to call
String yourServiceUrl = "http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=LN11RX";

@Override

protected void onPreExecute() {
}

@Override

protected String doInBackground(String... arg0) {

try {
// create new instance of the httpConnect class
httpConnect jParser = new httpConnect();

// get json string from service url
String json = jParser.getJSONFromUrl(yourServiceUrl);

// save returned json to your test string
jsonTest = json.toString();

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String strFromDoInBg) {
TextView tv1 = (TextView) findViewById(R.id.jsontext);
tv1.setText(jsonTest);
}

}

}

我的 httpConnect 类来处理 URL:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;


public class httpConnect {

final String TAG = "JsonParser.java";

static String json = "";

public String getJSONFromUrl(String url) {

try {

URL u = new URL(url);
HttpURLConnection restConnection = (HttpURLConnection) u.openConnection();
restConnection.setRequestMethod("GET");
restConnection.setRequestProperty("Content-length", "0");
restConnection.setUseCaches(false);
restConnection.setAllowUserInteraction(false);
restConnection.setConnectTimeout(10000);
restConnection.setReadTimeout(10000);
restConnection.connect();
int status = restConnection.getResponseCode();

// switch statement to catch HTTP 200 and 201 errors
switch (status) {
case 200:
case 201:

BufferedReader br = new BufferedReader(new InputStreamReader(restConnection.getInputStream()));

// create a new string builder to store json data returned from the REST service
StringBuilder sb = new StringBuilder();
String line;

// loop through returned data line by line and append to stringbuilder 'sb' variable
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();

try {
json = sb.toString();
} catch (Exception e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
return json;
}
// HTTP 200 and 201 error handling from switch statement
} catch (MalformedURLException ex) {
Log.e(TAG, "Malformed URL ");
} catch (IOException ex) {
Log.e(TAG, "IO Exception ");
}
return null;
}

那么有没有办法对返回的数据进行排序并将每一位数据放入自己的文本框中?

JSON 截图:

enter image description here

最佳答案

如果我正确理解你的问题,请尝试给出此 link走吧。如果我误解了,请告诉我,我会尽力帮助您提供替代方案。

编辑:动态创建新的 TextView 并设置数据的大致另一种方法:

TextView view;
LinearLayout currLayout = (LinearLayout) findViewById(R.id.LinearLayout);
for(String value : items) {
view = new TextView();
view.setText(value);
currLayout.addView(view);
}

关于java - Android:对 JSON 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253570/

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