【问题标题】:How can I POST "string request" and get data?如何发布“字符串请求”并获取数据?
【发布时间】:2016-07-11 12:35:07
【问题描述】:

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

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

例如我的网址是=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;

那么我怎样才能发布这些字符串并获取我的 json 日期。

问候。

【问题讨论】:

标签: android json web-services


【解决方案1】:

我已经解决了:)

搜索了几个小时,尝试了很多想法,终于得到了它:)

如果你有一个获取 json 数据的参数,例如你有一个天气 api,你需要某个地方的天气。

这样解释如何在 API URL 上发送字符串请求和获取 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());
    }
}

感谢大家帮助我:)

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多