public class RequestHandler {
public String sendPostRequest(String requesURL, HashMap<String,String> postDataParams){
URL url;
StringBuilder sb=new StringBuilder();
try{
url=new URL(requesURL);
HttpsURLConnection conn=(HttpsURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
OutputStream os=conn.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(os,"UTf-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if(responseCode == HttpsURLConnection.HTTP_OK){
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb=new StringBuilder();
String response;
while ((response=br.readLine())!=null){
sb.append(response);
}
}
}catch(Exception e){
e.printStackTrace();
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> postDataParams) throws UnsupportedEncodingException {
StringBuilder result=new StringBuilder();
boolean first=true;
for(Map.Entry<String,String> entry: postDataParams.entrySet()){
if(first)
first=false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
И вот я получаю данные
class RegisterUser extends AsyncTask<Void, Void, String> {
private ProgressBar progressBar;
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
HashMap<String, String> params = new HashMap<>();
params.put("FirstName",fname);
params.put("LastName",lname);
params.put("Contact",cNumber);
params.put("EmailId",email);
params.put("Password",password);
params.put("CountryCode",cCode);
return requestHandler.sendPostRequest(URLs.URL_REGISTER, params);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//displaying the progress bar while user registers on the server
progressBar = (ProgressBar) findViewById(R.id.progressBarId);
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s !=null) {
progressBar.setVisibility(View.GONE);
try {
JSONObject obj = new JSONObject(s);
JSONArray fnameArray = obj.getJSONArray("FirstName");
String fname = fnameArray.getString(0);
JSONArray lNameArray = obj.getJSONArray("LastName");
String lname = lNameArray.getString(0);
JSONArray contArray = obj.getJSONArray("Contact");
String contact = contArray.getString(0);
JSONArray eMailIdArray = obj.getJSONArray("EmailId");
String email = eMailIdArray.getString(0);
// JSONArray passwordArray=obj.getJSONArray("Password");
// String password=passwordArray.getString(0);
JSONArray countryCodeArray = obj.getJSONArray("CountryCode");
String countryCode = countryCodeArray.getString(0);
User user = new User(fname, lname, contact, email, countryCode);
SharedPrefMannager.getInstance(getApplicationContext()).userLogin(user);
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
} catch (Exception e) {
e.printStackTrace();
Log.e("catch", "" + e);
Toast.makeText(MainActivity.this, e.toString() + "this is onPost", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "unable to register", Toast.LENGTH_SHORT).show();
}
}
}
RegisterUser ru = new RegisterUser();
ru.execute();
}
}
И мой JSON здесь:
{
"Contact": [
"Contact Should Not be Null"
],
"EmailId": [
"EmailId Should Not be Null"
],
"LastName": [
"Last Name Should Not be Null"
],
"Password": [
"Password Should Not be Null"
],
"FirstName": [
"First Name Should Not be Null"
],
"CountryCode": [
"Country Code Should Not be Null"
]
}