I am getting the JSON response from PHP as {"success":'Hi Namrata...'} when the details are filled out in the editText field. I want to read the success value and display it in the new activity and in case of {"error":'something happened'} I want to be on the same activity.. How can I do that. Here is my MainActivity
public class MainActivity extends AppCompatActivity {
public Button blogin;TextView content;
public EditText uname, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText) findViewById(R.id.uname);
pass = (EditText) findViewById(R.id.pass);
content= (TextView)findViewById( R.id.content );
blogin = (Button) findViewById(R.id.blogin);
blogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JsonTask().execute("https://www.aboutmyclinic.com/test.php",uname.getText().toString(),pass.getText().toString());
}
});
}
public class JsonTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("firstParam", params[1])
.appendQueryParameter("secondParam", params[2]);
//.appendQueryParameter("type", params[3]);
String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
InputStream inputStream = connection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
content.setText(result);
}
}
}