Let me describe my application, I am extracting data from the JSON url website (Drupal website), the data is in JSON format. In my login function, it works fine. and the user is checked on the server. I also retrieve other data (JSON url) from the server and display it in an Android app.
Now the problem is that I cannot access JSON data on pages where login is required, because my login is not supported in the whole Android application.
I searched on stackoverflow and google. I got these links and tried, but don’t know how to use them in my code:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
Android Session Management
Android Http File Storage
Here is the empty JSON that is on the drupal site without login.
{
"nodes": []
}
Here is the JSON from drupal - after logging in ( http://www.mywebsite.com/user/login ) and reload the page http://www.mywebsite.com/myaccount-page on the web browser website . means the computer web browser automatically supports a login session.
{
"nodes": [
{
"node": {
"Uid": "51",
"Username": "anand",
"Name": "anand",
"Address": "\n\tAt- vadodara Nr. Kareli Baugh",
"Date of Birth": "1998-08-20",
"Occupation": "student",
"Member Since": "36 weeks 6 days"
}
}
]
}
But in an Android app, it does not do this automatically.
So, I want to save this session on Android so that I can log into the Android application after you go to another activity page and get the JSON data. Here is my code:
LoginActivity.java
public void onClick(View v) {
String uName = editUser.getText().toString();
String Password = editPass.getText().toString();
if(uName.equals("") | Password.equals(""))
{
Toast.makeText(getApplicationContext(), "Enter the Username and Password",Toast.LENGTH_SHORT).show();
}
else{
String strResponse = util.makeWebCall(loginURL,uName,Password);
System.out.println("=========> Response from login page=> " + strResponse);
try{
if (strResponse.substring(KEY_SUCCESS) != null) {
txterror.setText("");
Intent inlogin = new Intent(LoginActivity.this,
post_myprofile.class);
inlogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inlogin);
}
else
{
txterror.setText("Username and Password Not valid !!!");
}
}
catch (Exception e) {
}
}
}
});
btngotoregister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(intent1);
}
});
}
}
makeWebCall method in util.java
util.java
public static String makeWebCall(String url, String uname,String pass)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",uname));
params.add(new BasicNameValuePair("password",pass));
UrlEncodedFormEntity formEntity = null;
try {
formEntity = new UrlEncodedFormEntity(params);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
post.setEntity(formEntity);
try {
HttpResponse response = client.execute(post);
System.out.println("=========> Responsehello => "+response);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
return iStream_to_String(is);
}
else
{
return "Hello This is status ==> :"+String.valueOf(statusCode);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
, JSON .
& ; - .
2- JSON userprofile. , Blank JSON, .
2- -.
post_myprofile.java
protected Void doInBackground(Void... params) {
String url = "http://www.cheerfoolz.com/myaccount-page";
String strResponse = util.makeWebCall(url);
try {
JSONObject objResponse = new JSONObject(strResponse);
JSONArray jsonnodes = objResponse
.getJSONArray(API.cheerfoolz_myprofile.NODES);
makewebcall util.java
util.java
public static String makeWebCall(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet(url);
try {
HttpResponse httpResponse = client.execute(httpRequest);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = httpResponse.getEntity();
InputStream is = entity.getContent();
return iStream_to_String(is);
}
catch (IOException e) {
httpRequest.abort();
}
return null;
}
public static String iStream_to_String(InputStream is1)
{
BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
String line;
StringBuilder sb = new StringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
}
JSON , , - . ?
.