Performing get/post from server for JSON response

2019-08-25 08:59发布

What is the most efficient method to get a JSON response when using http get/post. Obviously they must be done asynchronously.

Note: I already have internet permissions enabled in the manifest file.

Posting:

HttpPost httpPost = new HttpPost("MYDOMAIN");
HttpResponse response = httpClient.execute(httpPost);

Getting:

HttpGet httpGet = new HttpGet("MYDOMAIN");
HttpResponse response = httpClient.execute(httpGet);

1条回答
beautiful°
2楼-- · 2019-08-25 09:13

HTTP Requests have to be done Asynchronously. First make sure you have INTERNET Permission in your AndroidManifest.xml

Then make a class for Request so you can reuse it

public class Request extends AsyncTask<List<NameValuePair>, Void, String> {


    Callback.JSONCallback callback;
    String url;
    String type;

    public Request(String type, String url, Callback.JSONCallback callback) {
        this.callback = callback;
        extension = url;
        this.type = type;
    }

    // What to do Async, in this case its POST/GET
    protected String doInBackground(List<NameValuePair>... pairs) {

        HttpClient httpClient = new DefaultHttpClient();

        if (type.equals("POST")) {
            HttpPost httpPost = new HttpPost(url);

            try {
                // Add your data
                httpPost.setEntity(new UrlEncodedFormEntity(pairs[0], "UTF-8"));

                // Execute HTTP Post Request
                HttpResponse response = httpClient.execute(httpPost);
                String result = EntityUtils.toString(response.getEntity());

                return result;

            } catch (Exception e) {
                Log.v("error", e.toString());
            }
        } else if (type.equals("GET")) {
            try {
                HttpGet httpGet = new HttpGet(url);
                HttpResponse response = httpClient.execute(httpGet);
                response.getStatusLine().getStatusCode();

                String result = EntityUtils.toString(response.getEntity());

                return result;

            } catch (Exception e) {
                Log.v("error", e.toString());
            }
        }

        return "";

    }

    // What to do after AsyncTask
    protected void onPostExecute(String feed) {
        JSONObject JSON = null;
        try {
            JSON = new JSONObject(feed);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        callback.call(JSON);
    }

}

Then make a class called callback and make an interface like so:

public class Callback {
    public interface JSONCallback {
        void call(JSONObject JSON);
    }

}

Then either use POST or GET. Server should return JSON and then you can parse it as you wish

List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
// Not manditory, can be used for things like token, etc.
nameValuePairList.add(new BasicNameValuePair("ID", "VALUE"));
new Request("POST", "URL", new Callback.JSONCallback() {
    @Override
    public void call(JSONObject JSON) {
        try {
            // Parse JSON here

        } catch (JSONException e) {
            Log.v("error", e.toString());
        }
    }
}).execute(nameValuePairList);

List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
// Not manditory, can be used for things like token, etc.
nameValuePairList.add(new BasicNameValuePair("ID", "VALUE"));
new Request("GET", "URL", new Callback.JSONCallback() {
    @Override
    public void call(JSONObject JSON) {
        try {
            // Parse JSON here

        } catch (JSONException e) {
            Log.v("error", e.toString());
        }
    }
}).execute(nameValuePairList);
查看更多
登录 后发表回答