मुझे अपने आवेदन का वर्णन करने दें, मैं वेबसाइट JSON url (Drupal वेबसाइट) से डेटा प्राप्त कर रहा हूं, डेटा JSON प्रारूप में है। मेरे आवेदन लॉगिन कार्यक्षमता पूरी तरह से काम करता है। & उपयोगकर्ता सर्वर पर मान्य है। मैं अपने एंड्रॉइड एप्लिकेशन में प्रदर्शित & सर्वर से अन्य डेटा (जेएसओएन यूआरएल) भी ला रहा हूं। अब, समस्या यह है कि मैं पृष्ठों के JSON डेटा तक नहीं पहुंच सकता, जहां लॉगिन आवश्यक है, क्योंकि मेरा लॉगिन पूरे एंड्रॉइड एप्लिकेशन को बनाए रखता नहीं है।एंड्रॉइड देशी एप्लिकेशन में सर्वर लॉगिन कैसे बनाए रखें?
मैं stackoverflow & गूगल पर खोज की है मैं इन कड़ियों & की कोशिश की हो गई लेकिन कैसे मेरे कोड में उन्हें इस्तेमाल करना नहीं आता: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
यहाँ रिक्त JSON है जो लॉगिन के बिना ड्रूपल साइट से है। कंप्यूटर webbrowser में -
{
"nodes": []
}
यहाँ Drupal साइट- लॉगिन (http://www.mywebsite.com/user/login) के बाद से JSON & साइट पर पेज http://www.mywebsite.com/myaccount-page पुनः लोड है। मतलब कंप्यूटर वेब ब्राउज़र स्वचालित रूप से लॉगिन सत्र को बनाए रखता है।
{
"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"
}
}
]
}
लेकिन एंड्रॉयड आवेदन में यह स्वचालित रूप से ऐसा नहीं करती है। तो मैं एंड्रॉयड में इस सत्र बनाए रखने के लिए इतना है कि मैं एंड्रॉयड आवेदन में प्रवेश कर सकते हैं, लॉगिन एक और पेज-गतिविधि को अनुप्रेषित के बाद & वहाँ JSON डेटा प्राप्त करना चाहते हैं। यहाँ मेरी कोड है:
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);
//finish();
}
else
{
txterror.setText("Username and Password Not valid !!!");
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
});
btngotoregister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(),
RegisterActivity.class);
// intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
}
});
}
}
इस कोड के साथ
makeWebCall विधि 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) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
post.setEntity(formEntity);
try {
//post.setEntity(new StringEntity(requestString));
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
अब लॉगिनसफल हैमुझे सर्वर से जेएसओएन प्रतिक्रिया विस्तार से मिली है। & पृष्ठ-गतिविधि उपयोगकर्ता प्रोफ़ाइल के लिए दूसरे पृष्ठ पर रीडायरेक्ट करता है। दूसरे पृष्ठ पर मुझे उपयोगकर्ता प्रोफाइल जेएसओएन डेटा नहीं मिल रहा है-जैसा ऊपर बताया गया है, मुझे खाली JSON मिल रहा है क्योंकि सत्र बनाए रखा नहीं गया है।
यहां दूसरे पृष्ठ-गतिविधि का कोड है।
post_myprofile।जावा
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
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);
// HttpPost post = new HttpPost(url);
try {
HttpResponse httpResponse = client.execute(httpRequest);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
/* Log.i(getClass().getSimpleName(),
"Error => " + statusCode + " => for URL " + url);*/
return null;
}
HttpEntity entity = httpResponse.getEntity();
InputStream is = entity.getContent();
return iStream_to_String(is);
}
catch (IOException e) {
httpRequest.abort();
// Log.w(getClass().getSimpleName(), "Error for URL =>" + url, e);
}
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
}
मैं इस पृष्ठ में यहां खाली JSON हो रही है - कि मैं ऊपर उल्लेख किया है। तो इस उपयोगकर्ता प्रोफाइल गतिविधि में सत्र को बनाए रखने के लिए कैसे करें & डेटा प्राप्त करें?
सुनने के लिए धन्यवाद।
आपको बहुत बहुत धन्यवाद .. यह बहुत मदद की थी :) – Dave