1

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;
    }

}

bharath
  • 623
  • 7
  • 30
msk
  • 21
  • 3
  • 1
    in onPostExecute you will return your login response, from where you can parse the response and move for second JSON. – Silvans Solanki Mar 10 '16 at 12:32
  • please tell me how to parse the response and move to Second Json in post execute with simple example...because i m not getting any data from 2nd url if i parse in post execute....please help me with simple example.... – msk Mar 10 '16 at 16:16
  • please any one answer my question.... – msk Mar 11 '16 at 04:53

1 Answers1

0

There are many ways to parse a JSON string into an object representation.

I see you already use JSONObject to create the body of your POST request. You can use new JSONObject(jsonString) to create an object from the response's body, after you've converted it to a String with convertInputStreamToString.

However there are better alternatives that will map the JSON to your own object model, e.g. the Gson library.

Have a look at this answer https://stackoverflow.com/a/31743324/2914666 or any of the related questions listed on the side.

Community
  • 1
  • 1
Philippe A
  • 1,252
  • 9
  • 22