Java从URL

时间:2017-09-06 06:26:44

标签: java android json

我想从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();
    }
}

谁能看到我做错了什么?网址的回复是 {"saldo":783}

2 个答案:

答案 0 :(得分:1)

我认为问题出现在try块的主要功能

JSONObject jsonObject = new JSONObject();
String response = jsonObject.getString("saldo");
txtUser.setText(response);

您的jsonObject为空您应该致电

String response = json.getString("saldo");
txtUser.setText(response);

当您进行网络调用时,还应该在后台线程中执行此操作,而不是在UI线程上执行。(readJsonFromUrl方法应在后台线程中调用)

如NguyễnTrungHiếu的回答所示

答案 1 :(得分:1)

试试吧!

除去

   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();
    }
}