2017-10-31 1 views
0

Ich bin neu bei Android und ich verwende O-Authentifizierung 1.0 in meinem Code. Anfrage erhalten funktioniert, aber Post-Anfrage funktioniert nicht in meinem Code. Wenn ich einen Postboten einchecke, erhalte ich eine Antwort, bekomme aber keine Anfrage im Code. Hier ist mein Code: protected String doInBackground (String ... params) {Android OAuth 1.0 Post Anfrage

 try { 
      URL url = new URL(params[0]); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setDoInput(true); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
      httpURLConnection.setRequestMethod("POST"); 


      // Send the post body 
      if (this.postData != null) { 
       OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream()); 
       writer.write(postData.toString()); 
       writer.flush(); 
      } 

      int statusCode = httpURLConnection.getResponseCode(); 

      if (statusCode == 200) { 

       InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream()); 

      } else { 
       // Status code is not 200 
       // Do something to handle the error 
      } 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

Vielen Dank im Voraus.

Antwort

0

Sie könnten diese für Rest-API-Anfragen verwenden.

http://square.github.io/retrofit/

http://square.github.io/okhttp/

Es Ihre Codelänge und Linien reduzieren und macht effizient Ihre Sachen schneller geladen und Bandbreite spart. Es unterstützt sowohl synchrone blockierende Aufrufe als auch asynchrone Aufrufe mit Rückrufen.

+0

Es ist hilfreich, aber ich bekomme nicht, wie man O-Authentication 1.0 benutzt. Wie kann ich es in meinem Code verwenden? Kannst du bitte ein Beispiel geben, um das zu erklären? – Risk

+0

können Sie es tun, indem Sie Interceptor hinzufügen, um anzufordern. Gehen Sie über diesen Link https://stackoverflow.com/questions/18478258/android-retrofit-parameterized-headers – JosephM