2016-04-11 12 views
4

Ich hatte Wechsel von API23 bis 22, weil sie sagten Httpclient nicht verfügbar.Bei ich ich hatte Probleme mit Httpclient, Httppost API22 geschaltet und NameValuePair.I fand die Lösung zu verwenden HttpURLConnectionHandler.But I Ich weiß nicht, wie ich es für die folgende Methode verwenden soll.Wie Daten schreiben mit HttpURLConnection

public void send(View v) 
{ 

    HttpClient httpclient = new DefaultHttpClient(); 
    // put the address to your server and receiver file here 
    HttpPost httppost = new HttpPost("http://yoursite/yourPHPScript.php"); 
    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     // we wont be receiving the parameter ID in your server, but it is here to show you how you can send more data 
     nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
     // message is the parameter we are receiving, it has the value of 1 which is the value that will be sent from your server to your Arduino board 
     nameValuePairs.add(new BasicNameValuePair("message", "1")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     httpclient.execute(httppost); // send the parameter to the server 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

Jemand freundlicherweise mir helfen

+0

Was Sie genau zu tun versuchen, und was ist speziell darauf, dass Sie Schwierigkeiten haben, mit? (Welche Fehlermeldungen erhalten Sie.) –

+0

HttpPost, HttpClient und NameValuePair sind veraltet. Ich muss diese Post-Methode mit der HttpURLConnectionHandler-Klasse verwenden, die ich bereits habe @ChrisBritt – Shrei

Antwort

4

Sie können so etwas tun:

public boolean sendPost(MessageSenderContent content) { 

     HttpURLConnection connection; 
     try { 
      URL gcmAPI = new URL("your_url"); 
      connection = (HttpURLConnection) gcmAPI.openConnection(); 

      connection.setRequestMethod("POST");// type of request 
      connection.setRequestProperty("Content-Type", "application/json");//some header you want to add 
      connection.setRequestProperty("Authorization", "key=" + AppConfig.API_KEY);//some header you want to add 
      connection.setDoOutput(true); 

      ObjectMapper mapper = new ObjectMapper(); 
      mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); 
      DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream()); 
      //content is the object you want to send, use instead of NameValuesPair 
      mapper.writeValue(dataOutputStream, content); 

      dataOutputStream.flush(); 
      dataOutputStream.close(); 

      responseCode = connection.getResponseCode(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (responseCode == 200) { 
      Log.i("Request Status", "This is success response status from server: " + responseCode); 
      return true; 
     } else { 
      Log.i("Request Status", "This is failure response status from server: " + responseCode); 
      return false; 
     } 
} 

Beispiel für MessageSenderContent, als Sie Ihre eigenen für "Botschaft" erstellen und "id":

public class MessageSenderContent implements Serializable { 
    private List<String> registration_ids; 
    private Map<String, String> data; 

    public void addRegId(String regId){ 
     if (registration_ids==null){ 
      registration_ids = new LinkedList<>(); 
      registration_ids.add(regId); 
     } 
    } 

    public void createData(String title,String message) { 
     if (data == null) 
      data = new HashMap<>(); 

     data.put("title", title); 
     data.put("message", message); 
    } 

    public Map<String, String> getData() { 
     return data; 
    } 

    public void setData(Map<String, String> data) { 
     this.data = data; 
    } 

    public List<String> getRegIds() { 
     return registration_ids; 
    } 

    public void setRegIds(List<String> regIds) { 
     this.registration_ids = regIds; 
    } 

    @Override 
    public String toString() { 
     return "MessageSenderContent{" + 
       "registration_ids=" + registration_ids + 
       ", data=" + data + 
       '}'; 
    } 

UPDATE:

Sie können HttpURLConnection verwenden nach dem Import dies in Ihrer build.gradle Datei

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.0" 
    useLibrary 'org.apache.http.legacy' // this one will let you use HttpUrlConnection 
    packagingOptions { 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/NOTICE.txt' 
} 
    ... 
} 
+0

Vielen Dank! Kann ich auch die HttpURLConnection-Klasse erhalten, die Sie verwendet haben? – Shrei

+0

Können Sie den HttpURLConnection-Code auch posten? – Shrei

+0

Es ist eine Klasse, die Sie nach dem Import einer Bibliothek verwenden können. Ich aktualisierte meine Antwort ... –