2016-07-11 5 views
0

Ich habe eine Anwendung auf Android 23 versucht und ich steckte irgendwo.Wie kann ich "String Request" POST und Daten bekommen?

Ich habe eine Anfrage, aber seine kein URL-Format! !!! Sein String-Format. Ich poste auf dem Server und gebe Daten zurück.

für Beispiel meine url = ist http://api.someurl/app_dev.php/tr/content

Und ich brauche ein paar String-Parameter schreiben wie

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

und es sollte einige JSON-Daten zurück. Weil diese Anfrage ist Suchparameter!

mein Code

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 Wie kann ich diesen String posten und mein json Datum.

Grüße.

+0

Sie Dritten wie 'Retrofit' verwenden sollte oder 'Volley' etwas :) – AndiGeeky

+0

Ich glaube, dies Ihre Frage beantwortet: http: // Stackoverflow. com/questions/4205980/java-sending-http-parameter-via-post-methode-leicht # answer-4206094. – Desirius

+0

Mögliches Duplikat von [Wie sende ich ein JSON-Objekt über Anfrage mit Android?] (Http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android) – JEY

Antwort

0

Ich habe Gelöst :)

Gesuchter viele Stunden versuchten viele denkt und schließlich wurde es :)

Wenn Sie einen Parameter für get json Daten haben zum Beispiel haben Sie ein Wetter api und Du brauchst irgendwo Wetter.

Auf diese Weise erklären Wie sendet String Request auf API URL und Get Json Daten.

Für Beispiel meine Anfrage, müssen

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

ich diese Info für meine Daten.

Nach all dem ist mein Code Arbeit.

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()); 
    } 
} 

Vielen Dank allen für mich geholfen :)