简体   繁体   中英

How to make a http request from Android?

I am trying to make a mobile app and I'm currently creating the api for communicating with the server. My code works fine on a normal maven project but when I add it to my android studio project I get errors when I try to read from the connection input stream.

package client;

import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class CCH {

    //static private String host = "http://localhost:8080";
    static private String host ="https://radiant-bayou-97811.herokuapp.com";

    //GET REQUESTS
    public static Integer login(String username, String password){
        String tempurl = host + "/api/user/login/" + username + "/" + password;
        JSONObject jsonObject = getRequest(tempurl);
        if(jsonObject.has("login"))
            if(jsonObject.get("login").equals("true"))
                return 1;
            else
                return 0;
        else
            return 0;
    }
    public static JSONObject getPacient(String username){
        String tempurl = host + "/api/user/getPacient/" + username;
        return getRequest(tempurl);
    }
    public static JSONObject getIstoric(String username){
        String tempurl = host + "/api/user/pacient/istoric/" + username;
        return getRequest(tempurl);
    }
    public static JSONObject getDiagnostic(String username){
        String tempurl = host + "/api/user/pacient/diagnostic/" + username;
        return getRequest(tempurl);
    }
    //POST REQUESTS
    public static void sendData(String username,String time,String puls,String calorii,String nr_pasi,String nivel_oxigen,String calitate_somn){
        String tempurl = host+ "/api/user/pacient/importData";
        JSONObject jsonObject = new JSONObject();
        jsonObject.append("username",username);
        jsonObject.append("time",time);
        jsonObject.append("puls",puls);
        jsonObject.append("calorii",calorii);
        jsonObject.append("nr_pasi",nr_pasi);
        jsonObject.append("nivel_oxigen",nivel_oxigen);
        jsonObject.append("calitate_somn",calitate_somn);

        postRequest(tempurl,jsonObject);
    }
    public static JSONObject getRequest(String URL){
        JSONObject response = null;
        HttpURLConnection connection = null;
        try {



            URL url = new URL(URL);

            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("GET");

            response = new JSONObject(readFromConnection(connection));




        }catch (Exception e){
            response = new JSONObject();
            response.append("Error!","Error on the client side contact the admin!");
            response.append("Error!!",e.getCause());
            e.printStackTrace();
        }
        finally {
            connection.disconnect();
            return response;
        }
    }
    public static void postRequest(String URL,JSONObject jsonObject){
        HttpURLConnection connection = null;
        try{
            URL url = new URL(URL);

            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
//            connection.setRequestProperty("Accept", "application/json");

            connection.setDoOutput(true);

            OutputStream os = connection.getOutputStream();
            os.write(createJson(jsonObject).getBytes());
            System.out.println(createJson(jsonObject));
            os.flush();
            os.close();
            int responseCode = connection.getResponseCode();
        } catch (Exception e){
            System.out.println("Unable to make post request!");
            System.out.println(e.getCause());
            System.out.println(e.getStackTrace());
            System.out.println(URL);
        }
    }
    public static String readFromConnection(HttpURLConnection connection) throws IOException {
        StringBuilder content;
        BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        content = new StringBuilder();
        while ((line = input.readLine()) != null) {
            content.append(line);
        }
        return content.toString();
    }
    public static String createJson(JSONObject jsonObject){
            StringBuilder tempString = new StringBuilder();
            tempString.append("{");
            int count = 0;
            for(String key : jsonObject.keySet()){
                if(count!=0)
                    tempString.append(",");
                tempString.append("\"");
                tempString.append(key);
                tempString.append("\"");
                tempString.append(":");
                String temp = jsonObject.get(key).toString();
                temp = temp.substring(1);
                temp = temp.substring(0,temp.length()-2);
                tempString.append(temp);
                tempString.append("\"");
                count++;
            }
            tempString.delete(tempString.length()-1,tempString.length()-1);
            tempString.append("}");
            return tempString.toString();
    }
}

I also tried using AsyncTask but I couldn't make it work.

public class LoginAsync extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {

        String tempurl = "https://radiant-bayou-97811.herokuapp.com" + "/api/user/login/" + "user" + "/" + "test";
        JSONObject jsonObject = getRequest(tempurl);
        System.out.println(jsonObject);
        System.out.println(tempurl);

        if (jsonObject.has("login")) {
            try {
                if (jsonObject.get("login").equals("true"))
                    return 1L;
                else
                    return 0L;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        else
            return 0L;
        return 0L;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(String result) {
          System.out.println("something");
        }

Then I just call the LoginAsync with some random url. I can't understand what I'm doing wrong.

Works fine on... whatever ... but not... Android ...

If the error only occurs while running code on Android Device (or emulated Device from Android Studio IDE) this hints that something with your android permission settings might be missing...

have you added <user-permission android:name="android.permission.INTERNET" /> to your AndroidManifest.xml?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM