Android: AsyncTask to make an HTTP GET Request?

前端 未结 7 691
失恋的感觉
失恋的感觉 2020-11-27 19:46

I\'m new to Android development. My question is, do I use AsyncTask in order to make an HTTP GET request (JSON response)? Is this correct? Does anyone know where I can see a

相关标签:
7条回答
  • 2020-11-27 20:29

    This is the code which conduct the post request and requests server in the form of json.

    {"emailId":"test123@gmail.com","emailIdOTP":"123456","phoneNo":"1111111111"}
    

    AsyncTask:

    public class GetAsynTask extends AsyncTask<String, String, String> {
    
            @Override
            protected String doInBackground(String... params) {
    
                try {
                    // Creating & connection Connection with url and required Header.
                    URL url = new URL(JWT_URL);
                    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                    urlConnection.setRequestProperty("Content-Type", "application/json");
                    urlConnection.setRequestMethod("POST");   //POST or GET
                    urlConnection.connect();
    
                    // Create JSONObject Request
                    JSONObject jsonRequest = new JSONObject();
                    jsonRequest.put("emailId", "test123@gmail.com");
                    jsonRequest.put("emailIdOTP", "123456");
                    jsonRequest.put("phoneNo", "1111111111");
    
    
                    // Write Request to output stream to server.
                    OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
                    out.write(jsonRequest.toString());
                    out.close();
    
                    // Check the connection status.
                    int statusCode = urlConnection.getResponseCode();
    
                    // Connection success. Proceed to fetch the response.
                    if (statusCode == 200) {
                        InputStream it = new BufferedInputStream(urlConnection.getInputStream());
                        InputStreamReader read = new InputStreamReader(it);
                        BufferedReader buff = new BufferedReader(read);
                        StringBuilder dta = new StringBuilder();
                        String chunks;
                        while ((chunks = buff.readLine()) != null) {
                            dta.append(chunks);
                        }
                        String returndata = dta.toString();
                        return returndata;
                    } else {
                        Toast.makeText(SplashActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                return null;
            }
    
            @Override
            protected void onPostExecute(String resultData) {
                super.onPostExecute(resultData);
                try {
                    JSONObject obj = new JSONObject(resultData);
                   String name= obj.getString("name");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题