Get JSON Data from URL Using Android?

后端 未结 8 2065
长发绾君心
长发绾君心 2020-11-28 04:48

I am trying to get JSON data by parsing login url with username and password. I have tried by using below code but I can\'t get any responses. Please help me.

I am u

8条回答
  •  一生所求
    2020-11-28 05:18

    Easy way to get JSON especially for Android SDK 23:

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

提交回复
热议问题