gpt4 book ai didi

Java 从 URL 获取 Android TextView 中的 JSON

转载 作者:行者123 更新时间:2023-12-02 02:37:30 24 4
gpt4 key购买 nike

我想从 Android Studio 中 TextView 中显示的 php 获取 JSON 响应。我现在有这段代码可以做到这一点,但它不起作用。据我所知,当应用程序打开时它甚至不运行。

public class MainActivity extends Activity {

private SQLiteHandler db;
private SessionManager session;

private String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}

public void main(String[] args) throws IOException, JSONException {
TextView txtUser = (TextView) findViewById(R.id.user);
JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
System.out.println(json.toString());
System.out.println(json.getString("saldo"));
try {
JSONObject jsonObject = new JSONObject();
String response = jsonObject.getString("saldo");
txtUser.setText(response);

} catch (JSONException e) {

e.printStackTrace();
}
}

有人能看出我做错了什么吗? url 的响应是{“saldo”:783}

最佳答案

试试吧!

删除

   JSONObject jsonObject = new JSONObject();

并使用

   JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
try {
String response = json.getString("saldo");
Log.e("AAAAAAAAA %s", response);

}

您必须在AsyncTask中调用它。已完成代码!!!

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
new GetDataSync().execute();
}

String saldo = "";

public class GetDataSync extends AsyncTask<Void, Void, Void> {


@Override
protected Void doInBackground(Void... params) {
try {
getData();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
txtUser.setText(saldo);
}
}

private void getData() throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://piggybank.wordmediavormgever.nl/getSaldo.php");
try {
String response = json.getString("saldo");
Log.e("AAAAAAAAA %s", response);
saldo = response;

} catch (JSONException e) {

e.printStackTrace();
}
}

private String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}

关于Java 从 URL 获取 Android TextView 中的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46068228/

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