我如何POST"字符串请求"并获取数据?

时间:2016-07-11 12:35:08

标签: android json web-services

我在Android 23上尝试了一个应用程序而且我卡在了某个地方。

我有一个请求,但不是网址格式! !!!它的字符串格式。我在服务器上发布并返回数据。

例如我的网址是= http://api.someurl/app_dev.php/tr/content

我需要发布一些字符串参数,如

 {
  "command":"read",
  "ctrl":"summaryOfDay",
  "data":{"date":"08.04.2016"},
  "order":null,
  "limit":null
 }

它应该返回一些json数据。因为此请求是搜索参数

我的代码是

HttpURLConnection connection=null;
BufferedReader reader=null;

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

    InputStream stream=connection.getInputStream();
    reader=new BufferedReader(new InputStreamReader(stream));
    StringBuffer buffer=new StringBuffer();
    String line = "";

    while ((line = reader.readLine())!= null){
        buffer.append(line);
    }
    String sJson = buffer.toString();
    JSONObject mjson = new JSONObject(sJson);




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

return null;

那么如何发布这些String并获取我的json日期。

问候。

1 个答案:

答案 0 :(得分:0)

我已解决了:)

搜索了很多个小时,尝试了许多人的思考并最终得到了它:)

如果你有一个获取json数据的参数,例如你有一个天气apı,你需要天气。

这种方式解释了如何在API URL和Get Json数据上发送字符串请求。

例如,我的请求是,

 {
  "command":"read",
  "ctrl":"summaryOfDay",
  "data":{"date":"08.04.2016"},
  "order":null,
  "limit":null
 }

我的数据需要此信息。

毕竟我的代码是Work。

private class downloadAPI extends AsyncTask<String,String,String>{

    String dateStr;

    ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Akış Bilgileri Getiriliyor...");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        Date timeNow = new Date();
        DateFormat df = new SimpleDateFormat("dd.MM.yyyy");

        dateStr=df.format(timeNow)+"";

        String data="{\"command\":\"read\",\"ctrl\":\"summaryOfDay\",\"data\":{\"date\":\""+dateStr+"\"},\"order\":null,\"limit\":null}";


        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestMethod("POST");
            connection.connect(); // it still works without this line, don't know why


            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(data);
            writer.close();
            os.close();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line = "";
            while ((line = reader.readLine())!= null){
                buffer.append(line);
            }
            String sJson = buffer.toString();
            JSONObject mjson = new JSONObject(sJson);

            return sJson;

        } 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(String result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        mText.setText(result.toString());
    }
}

感谢大家帮助我:)