मैं एक रेल अनुप्रयोग सर्वर से कनेक्ट करने का प्रयास कर रहा हूं जिसके लिए प्रमाणीकरण की आवश्यकता है। मैं डेस्कटॉप अनुप्रयोग पर जावा के लिए जकार्ता HTTP क्लाइंट का उपयोग कर रहा हूं और यह 100% काम करता है। लेकिन जब एंड्रॉइड एमुलेटर पर सटीक एक ही कोड निष्पादित किया जाता है तो मुझे एक IOException मिलता है।एंड्रॉइड फोन पर रेल सर्वर सर्वर पर मूल HTTP प्रमाणीकरण
यहां कोड है, और यदि कोई मुझे यह समझने में मदद कर सकता है कि यह आईओएक्सप्शन को क्यों फेंकता है जिसकी सराहना की जाएगी!
private boolean login()
{
String username, password;
DefaultHttpClient client;
AuthScope scope;
Credentials myCredentials;
CredentialsProvider provider;
HttpEntity entity;
String line;
BufferedReader reader;
InputStream instream;
//Declare & Create the HTTP Client
client = new DefaultHttpClient();
//Create our AuthScope
scope = new AuthScope("10.19.9.33", 3000);
username = "admin"
password = "pass"
//Set Credentials
myCredentials = new UsernamePasswordCredentials(username, password);
//Set Provider
provider = new BasicCredentialsProvider();
provider.setCredentials(scope, myCredentials);
//Set Credentials
client.setCredentialsProvider(provider);
String url = "http://10.19.9.33:3000/users/show/2";
HttpGet get;
//Tell where to get
get = new HttpGet(url);
HttpResponse response;
try
{
response = client.execute(get);
entity = response.getEntity();
/* Check to see if it exists */
if(entity != null)
{
instream = entity.getContent();
try {
reader = new BufferedReader(new InputStreamReader(instream));
line = reader.readLine();
if(line.equals("HTTP Basic: Access denied."))
return false;
while (line != null)
{
// do something useful with the response
System.out.println(line);
line = reader.readLine();
}
return true;
}
catch (IOException ex)
{
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
}
catch (RuntimeException ex)
{
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection and release it back to the connection manager.
get.abort();
throw ex;
}
finally
{
// Closing the input stream will trigger connection release
instream.close();
}
}
}
catch(ClientProtocolException cp_ex)
{
}
catch(IOException io_ex)
{
}
return false;
}
आप टी के प्रासंगिक अंश प्रदान कर सके वह ट्रेस ढेर? –
इनपुटस्ट्रीम के लिए एपीआई संदर्भ कहता है: यह अमूर्त वर्ग पूरी तरह से कार्यान्वित कार्यान्वयन प्रदान नहीं करता है, इसलिए इसे उप-वर्गीकृत करने की आवश्यकता है, और कम से कम पढ़ने() विधि को ओवरराइड करने की आवश्यकता है। BufferedInputStream आज़माएं। – techiServices
मुझे पता चला कि यह पता से कनेक्ट नहीं होने का कारण यह था कि मैं मैनिफेस्ट में इंटरनेट अनुमति जोड़ना भूल गया था। लेकिन, अब डिवाइस "प्रतिक्रिया = client.execute (प्राप्त) पर लटका हुआ प्रतीत होता है;" लाइन ... –