android - 使用 Android 从 URL 获取 JSON 数据?

标签 android json android-6.0-marshmallow

我正在尝试通过使用用户名和密码解析登录 URL 来获取 JSON 数据。我试过使用下面的代码,但我无法得到任何回应。请帮助我。

我正在使用 HTTP 进程和 API 级别 23。

我需要解析我的 URL 并获得下面的响应

{
    "response":{
                "Team":"A",
                "Name":"Sonu",
                "Class":"First",
              },
                "Result":"Good",
}

在我的代码下面:

public class LoginActivity extends Activity {

    JSONParser jsonparser = new JSONParser();
    TextView tv;
    String ab;
    JSONObject jobj = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        new retrievedata().execute();

    }

    class retrievedata extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            jobj = jsonparser.makeHttpRequest("http://myurlhere.com");

            // check your log for json response
            Log.d("Login attempt", jobj.toString());

            try {
                ab = jobj.getString("title");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return ab;
        }
        protected void onPostExecute(String ab){

            tv.setText(ab);
        }

    }

}

最佳答案

特别是 Android SDK 23 获取 JSON 的简单方法:

public class MainActivity extends AppCompatActivity {

Button btnHit;
TextView txtJson;
ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnHit = (Button) findViewById(R.id.btnHit);
    txtJson = (TextView) findViewById(R.id.tvJsonItem);

    btnHit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new JsonTask().execute("Url address here");
        }
    });


}


private class JsonTask extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Please wait");
        pd.setCancelable(false);
        pd.show();
    }

    protected String doInBackground(String... params) {


        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-) 

            }

            return buffer.toString();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pd.isShowing()){
            pd.dismiss();
        }
        txtJson.setText(result);
    }
}
}

关于android - 使用 Android 从 URL 获取 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229869/

相关文章:

android - StringBuilder 到字符串数组如何转换

java - Android - 空指针异常 : storage == null

java - Json 字符串到具有动态键名称的 Java 对象

安卓 6 : cannot share files anymore?

Android - 在 Android API 17 中使用指纹 API

android - 启动任何已安装的应用程序

android - ListView 不显示适配器

python - 检查键/值是否在 JSON 中

javascript - 在 Sharepoint 2010 webpart 中解析 json

android - 从 UsageStatsManager Android 获取以前日期的应用程序使用数据