如何使用httpConnectionUrl在发出POST请求时以jSON格式提供有效负载

时间:2016-04-01 10:07:50

标签: android json post

我正在尝试为测试网址发出POST请求 - " http://www.sample.com/interntest.php"有效负载为{"密码":" test12345"}

public class VisitorActivity extends AppCompatActivity {

private BitmapDrawable bitmapObject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_visitor);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    BitmapDrawable bitmapObject=null;
    new JSONTask().execute("http://www.sample.com/interntest.php");
    LinearLayout i = (LinearLayout)findViewById(R.id.backgroundImg);
    if (i != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            i.setBackground(bitmapObject);
        }
    }

}

public class JSONTask extends AsyncTask<String, String, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL(params[0]);
            connection= (HttpURLConnection) url.openConnection();
            String password="test12345";
            connection.setReadTimeout(10000);
            connection.setConnectTimeout(15000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            //connection.addRequestProperty("password", "test12345");
            DataOutputStream dbStrem= new DataOutputStream(connection.getOutputStream());
            dbStrem.writeBytes(password);
            connection.connect();
            dbStrem.flush();
            dbStrem.close();
            InputStream stream=connection.getInputStream();
            reader=new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line="";

            //StringBuilder responseOutput = new StringBuilder();
            /*String password="test12345";
            responseOutput.append("password: "+password);*/

            System.out.println("output===============");

            while((line=reader.readLine())!=null) {
                buffer.append(line);
            }
            String finalJSON= buffer.toString();
            JSONObject parentObject =  new JSONObject(finalJSON);
            Log.d("parentObject", String.valueOf(parentObject));
            String backgroundURL= parentObject.getString("backgroundURL");
            Log.d("IMGBACKGROUND",backgroundURL+backgroundURL);
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeStream((InputStream) new URL(backgroundURL).getContent());

            } catch (IOException e) {
                e.printStackTrace();
            }

            return bitmap;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if(connection!=null) {
                connection.disconnect();
            }
            if(reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

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

}

但是在这里我无法在httpUrlConnection中添加参数。我知道如何使用httpClient添加params,但不想使用它,因为它不再受支持。我可以知道更好的方法吗?

2 个答案:

答案 0 :(得分:0)

以下示例为您提供了有关改造库

的清晰概念

https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/

答案 1 :(得分:0)

使用Volley。这是一种推荐的方法。 Here是文档。