2016-04-25 7 views
1

seine NICHT DUPLICATE.Link das ist ein altes bereitgestellt wurde "http-Client" in api23Senden json Objekt über http post-Methode in android

Ich möchte Objekt senden json entfernt.:

{"emailId":"[email protected]","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"} 

zu url:

http://10digimr.mobimedia.in/api/mobile_retailer/update_profile Wie kann ich es tun? per Post-Methode?

METHODE:

POST /api/mobile_retailer/update_profile 

ZWINGENDEN SCHLÜSSEL:

{"emailId","address"} 

REQUEST JSON:

{"emailId":"[email protected]","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"91123546374208","bank_name":"Axis Bank","branch_name":"91123546374208","ifsc_code":"UTI0000879"} 

RESPONSE:

{"message":"Mail Send","data":true,"status":200} 
012 mit
+0

die api Sie erwähnt haben, ist eine GET-Methode. Fragen Sie den API-Entwickler nach einer Dokumentation über den korrekten Zugriff auf die API. Ich habe versucht, eine POSt-Anfrage über Rest-Client zu senden. Es gibt einen Fehler. – crashOveride

+1

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) –

+0

mein schlechtes! falsche URL wurde geschrieben! – user6092109

Antwort

5

Definieren Sie eine Klasse AsyncT und es in onCreate Methode aufrufen:

AsyncT asyncT = new AsyncT(); 
asyncT.execute(); 

Klassendefinition:

class AsyncT extends AsyncTask<Void,Void,Void>{ 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       URL url = new URL(""); //Enter URL here 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setRequestMethod("POST"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc. 
       httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json` 
       httpURLConnection.connect(); 

       JSONObject jsonObject = new JSONObject(); 
       jsonObject.put("para_1", "arg_1"); 

       DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
       wr.writeBytes(jsonObject.toString()); 
       wr.flush(); 
       wr.close(); 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 


    } 
+2

@gothdo Wie behandeln Sie die Antwort, die Sie von der Post-Nachricht erhalten? –