通过post发送数据而不使用apache类

时间:2016-03-17 20:07:43

标签: android

如果可能,我想在不使用http classes的情况下将数据发送到php服务器;即org.apache.http包。

我的代码到现在为止

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class serverHandler extends AsyncTask<String,Integer,String> {


    Context context;
    ByteArrayOutputStream content;

    public serverHandler(Context context){
        this.context = context;
    }
    @Override
    protected String doInBackground(String... params) {

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

            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            input = connection.getInputStream();

            byte buff[] = new byte[4096];
            content = new ByteArrayOutputStream();
            long total = 0;
            int count;
            while ((count = input.read(buff)) != -1) {
                // allow canceling with back button

                content.write(buff,0,count);
                total += count;

            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {

                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return new String(content.toByteArray());

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        String country = "No Country";

        if(result.length()==0){
            Toast.makeText(context, "No Data", Toast.LENGTH_LONG).show();
            return;
        }

        try{

            JSONObject data = new JSONObject(result);

            JSONObject address = data.getJSONObject("address");
            country = address.getString("country");


        }
        catch(JSONException e){
            e.printStackTrace();
        }
        Toast.makeText(context,country,Toast.LENGTH_LONG).show();
    }
}

我知道发送POST数据肯定需要一些HTTP服务。我要求的是,是否可以在不导入任何org.apache.http包的情况下发送数据。所有的SO答案和我在互联网上找到的例子都在使用这个软件包。而且,出于某种原因,如果可能的话,我也不想使用它。

1 个答案:

答案 0 :(得分:0)

您可以使用square retrofit库将POST数据发送到服务器。