0

I have tried a application on Android 23 and I stuck somewhere.

I have a request but its not a URL format!!!! Its String format. I post on Server and Return a data.

for Example my url is=http://api.someurl/app_dev.php/tr/content

And I need post some string parameter like

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

and it should return some json data. Because this request is search parameter !

My Code is

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;

So How can I post these String and get my json date.

Regards.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rıdvan
  • 720
  • 7
  • 11
  • You should use third party like 'Retrofit' or 'Volley' something :) – AndiGeeky Jul 11 '16 at 12:36
  • I believe this answers your question: http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily#answer-4206094. – Desirius Jul 11 '16 at 13:00
  • Possible duplicate of [How to send a JSON object over Request with Android?](http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android) – JEY Jul 11 '16 at 13:03
  • You need to parse the JsonObject? http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java or your JSON string response is not correct. What exactly is the problem? – OneCricketeer Jul 11 '16 at 13:05
  • I need send parameter and get json data @cricket_007 – Rıdvan Jul 11 '16 at 13:09
  • Gotcha. Yeah, Volley, OkHttp/Retrofit (or any other HTTP library) would be much simpler. – OneCricketeer Jul 11 '16 at 13:11

1 Answers1

0

I have Solved :)

Searched many hours, tried many thinks and finally got it :)

If you have a parameter for get json data for example you have a weather apı and you need somewhere Weather.

This way explain How is sending String Request on API URL and Get Json data.

For Example my Request is,

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

I need This Info for my data.

After all that my code is 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());
    }
}

Thanks everyone for helped me :)

Rıdvan
  • 720
  • 7
  • 11