I am working on one application ,in which i got two JSon url 1.(eg.www.xxxxxxx.com )to login application (by giving username and password) and 2.(eg.www.xxxxxx xxxx xxx.com )to get logged in user information,but we can get user information only after login,before login Second url does not show anything..so how to parse second json data from first json data in android... here is my Login.java(First json url to login)
public class Login extends Activity { private static final String SERVICE_URI = "http://xxxxxxxxxxxxxxxxxxxx.com/login"; private static Context mContext; public EditText edittext_username, edittext_password; Button button_submit,reg; public static final String TAG = "MYTAG"; Person person; static TextView txt_Error; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mContext = this; edittext_password = (EditText) findViewById(R.id.textEmailAddress); edittext_username = (EditText) findViewById(R.id.txtUsername); button_submit = (Button) findViewById(R.id.buttonLogin); txt_Error =(TextView)findViewById(R.id.txtError); button_submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { new MyAsyncTask().execute(); } }); } public String POST(String url, Person person){ InputStream inputStream = null; String result = ""; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); String json = ""; JSONObject jsonObject = new JSONObject(); jsonObject.accumulate("username", person.getName()); jsonObject.accumulate("password", person.getCountry()); json = jsonObject.toString(); StringEntity se = new StringEntity(json); httpPost.setEntity(se); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse httpResponse = httpclient.execute(httpPost); inputStream = httpResponse.getEntity().getContent(); if(httpResponse.getStatusLine().getStatusCode()==200){ Intent login = new Intent(mContext, MainActivity.class); mContext.startActivity(login); }else{ txt_Error.setText("Sorry!! Incorrect Username or Password"); } if(inputStream != null) result = convertInputStreamToString(inputStream); else result = "Did not work!"; } catch (Exception e) { Log.d("InputStream", e.getLocalizedMessage()); } return result; } private class MyAsyncTask extends AsyncTask<Void, Void, String> { ProgressDialog mProgressDialog; private DefaultHttpClient httpclient; private String username, password; private HttpPost httppost; private ArrayList<NameValuePair> nameValuePairs; private HttpResponse response; private HttpEntity entity; @Override protected void onPostExecute(String result) { mProgressDialog.dismiss(); } @Override protected void onPreExecute() { mProgressDialog = ProgressDialog.show(Login.this, "", "Loading..."); } @Override protected String doInBackground(Void... params) { httpclient = new DefaultHttpClient(); httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxxxx.com/login"); person = new Person(); person.setName(edittext_username.getText().toString()); person.setCountry(edittext_password.getText().toString()); return POST(SERVICE_URI, person); } } private static String convertInputStreamToString(InputStream inputStream) throws IOException{ BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); String line = ""; String result = ""; while((line = bufferedReader.readLine()) != null) result += line; inputStream.close(); return result; }
}